Does the C++ standard support processes?
Boost started supporting processes in version 1.64.0 (April 2017).
https://www.boost.org/doc/libs/1_70_0/doc/html/process.html
The fact that it is a Boost feature is a source of hope for its inclusion in a future C++ standard.
No, the c++ standard (particularly C++11) doesn't have any notion of a process (hence I can't give you a more reasonable reference here as a search result from the most popular and currently most up to date c++ documentation site).
I'm also not aware that process support is planned for the next standard version C++-17 (aka C++-1z). At least the Wikipedia Site doesn't mention it.
There is a popular implementation that was proposed for boost, but that never was drawn for a C++ standard proposal.
You usually can't write portable code to run on bare metal systems, where only one process exists.
However, is there a way to execute the
bar()
function in a separate process?
The simplest option to do that is to fallback to fork()
and wait()
as specified by the POSIX Open Group:
#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
void bar()
{
std::cout << "bar()\n";
}
int main(int argc, char **argv)
{
pid_t pid = fork();
if (pid == 0)
{
// child process
bar();
}
else if (pid > 0)
{
// parent process
wait(NULL);
}
else
{
// fork failed
std::cerr << "fork() failed!" << std::endl;
return 1;
}
return 0;
}
Though I don't see much of a point to create an extra process to execute a simple function. Creating a process creates a lot of overhead you don't want in such case.
Well, if you want to start another program using functions from the exec()
function family that's a different use case.
I'd recommend sticking to std::thread
s for your example.