|
Threads-Devel : |
Subject: [Threads-devel] [thread] notify of a condition variable doesn't work with a fork ?
From: nico (nicolas.guillot_at_[hidden])
Date: 2011-08-11 15:19:41
Hello.
I wrote a simple program that use the condition_variable class to
synchronize 2 threads, as explained here :
http://www.boost.org/doc/libs/1_47_0/doc/html/thread/synchronization.html#thread.synchronization.condvar_ref
When I fork a a given point (see example below), the wait of the
condition_variable never ends.
(I tried the code on ubuntu and fedora, and with boost 1.41 and boost 1.44).
I can't explain why, and how to solve (or workaround).
If I fork at the beginning of the program, it's ok.
Here follows the code,
the output is:
going to wait
End of processus of pid [5092]
and the code is:
#include <iostream>
#include "boost/thread.hpp"
#include "boost/bind.hpp"
#include <signal.h>
#include <cstdlib>
using namespace std;
boost::mutex mut;
boost::condition_variable cond_var;
bool cond;
void MyThread()
{
sleep(3);
{
boost::lock_guard<boost::mutex> lock(mut);
cond = true;
}
cond_var.notify_one();
}
int main()
{
pid_t pid;
cond = false;
boost::thread(boost::bind(MyThread));
pid = fork();
if (pid == 0)
{
boost::unique_lock<boost::mutex> lock(mut);
while (cond == false)
{
cout << "going to wait" << endl;
cond_var.wait(lock);
cout << "awake" << endl;
}
cout << "continue" << endl;
}
else
{
sleep(10);
}
cout << "End of processus of pid [" << pid << "]" << endl;
}
thanks for help!
Nicolas.