From: Kshitij Bansal Date: Wed, 28 Nov 2012 21:52:56 +0000 (+0000) Subject: fix a potential race (have failed to reproduce) X-Git-Tag: cvc5-1.0.0~7536 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=f0ebc08cf865d654a6d7ca4361775db8a64b1f62;p=cvc5.git fix a potential race (have failed to reproduce) --- diff --git a/src/main/portfolio.cpp b/src/main/portfolio.cpp index 4c542f548..e4b31f54d 100644 --- a/src/main/portfolio.cpp +++ b/src/main/portfolio.cpp @@ -25,9 +25,12 @@ namespace CVC4 { +/** Mutex to make sure at most one thread is winner */ boost::mutex mutex_done; + +/** Mutex / condition variable to notify main when winner data written */ boost::mutex mutex_main_wait; -boost::condition condition_var_main_wait; +boost::condition_variable condition_var_main_wait; bool global_flag_done; int global_winner; @@ -38,12 +41,16 @@ void runThread(int thread_id, boost::function threadFn, S& returnValue) returnValue = threadFn(); if( mutex_done.try_lock() ) { - if(global_flag_done == false) { - global_flag_done = true; - global_winner = thread_id; + if(global_flag_done == false) + { + { + boost::lock_guard lock(mutex_main_wait); + global_winner = thread_id; + global_flag_done = true; + } + condition_var_main_wait.notify_all(); // we want main thread to quit } mutex_done.unlock(); - condition_var_main_wait.notify_all(); // we want main thread to quit } } @@ -68,8 +75,9 @@ std::pair runPortfolio(int numThreads, if(not driverFn.empty()) thread_driver = boost::thread(driverFn); + boost::unique_lock lock(mutex_main_wait); while(global_flag_done == false) - condition_var_main_wait.wait(mutex_main_wait); + condition_var_main_wait.wait(lock); if(not driverFn.empty()) { thread_driver.interrupt();