Hi Gottlob, Thanks for your reply. I had written below code for this. void enable_flag (boost::mutex &mut, bool &flag) { mut.lock (); flag = 1; mut.unlock (); } void mywait (boost::condition_variable &cond, boost::mutex &mut, bool &flag) { boost::mutex tmp_mutex; boost::unique_lock<boost::mutex> lock(tmp_mutex); enable_flag (mut, flag); cond.wait (lock); } void notify_until_success (boost::condition_variable &cond, boost::mutex &mut, bool &flag) { for (;;) { usleep (500); mut.lock (); if (flag == 1) { cond.notify_one(); flag = 0; mut.unlock (); break; } mut.unlock (); } } But, the mistake I had done was, creation of 'tmp_mutex' inside mywait() and also I had not carefully read the documentation on wait() which says "Atomically call lock.unlock() and blocks the current thread." I read this while browsing for scoped_lock(). The code I have now written is as follows (which seems to work fine) : void enable_flag (boost::mutex &mut, bool &flag) { mut.lock (); flag = 1; mut.unlock (); } void mywait (boost::condition_variable &cond, boost::mutex &mut, boost::mutex &flag_mut, bool &flag) { boost::unique_lock<boost::mutex> lock(mut); enable_flag (flag_mut, flag); cond.wait (lock); } void notify_until_success (boost::condition_variable &cond, boost::mutex &mut, boost::mutex &flag_mut, bool &flag) { for (;;) { mut.lock (); flag_mut.lock (); if (flag == 1) { cond.notify_one(); flag = 0; flag_mut.unlock (); mut.unlock (); break; } flag_mut.unlock (); mut.unlock (); } } void worker_function (void) { for (;;) { /* wait for the new job */ mywait (new_job_condition, new_job_mutex, new_job_notification_reached_flag_mutex, new_job_notification_reached_flag); ..... read new job ..... ..... do the work ..... /* notify job done */ notify_until_success (job_done_condition, job_done_mutex, job_done_notification_reached_flag_mutex, job_done_notification_reached_flag); } } int main (void) { ..... create worker threads ..... while (/*there is work to be done */) { /* notify any one of the free threads about new_job */ notify_until_success (new_job_condition, new_job_mutex, new_job_notification_reached_flag_mutex, new_job_notification_reached_flag); /* wait for job_done notification from any of the busy thread */ mywait (job_done_condition, job_done_mutex, job_done_notification_reached_flag_mutex, job_done_notification_reached_flag); } } I hope this will not create any problems. (I have also edited the problem statement in the mail below to make it more clear. Shown by __TEXT_EDITED__.) Regards,
Girish
--- On Fri, 1/15/10, Gottlob Frege <gottlobfrege@gmail.com> wrote:
|