#include <iostream>
#include <chrono>
#include <thread>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int x = 100;
void wait_all()
{
int pid;
int status;
for (;;)
{
pid = wait(&status);
if (pid == -1)
{
if (errno == ECHILD)
{
break;
}
perror("wait");
exit(-1);
}
}
}
int main()
{
int pid = fork();
if (pid < 0)
{
perror("fork");
exit(-1);
}
if (pid == 0)
{
std::this_thread::sleep_for(std::chrono::seconds(2));
x = 200;
std::cout << "return child process:" << getpid() << ",x=" << x << std::endl;
}
else
{
std::cout << "return parent process:" << getpid() << ",x=" << x << std::endl;
}
wait_all();
return 0;
}
__sync_synchronize()
原语是一个内存屏障,它限制编译器和CPU对指令乱序执行的优化;在某些情况下,只限制编译器对指令优化,可以使用barrier()
原语。