c++ - The conditional variable is not working but after adding std::cout, it is working -


my project consists of 2 threads: 1 main thread , other thread handles window content. so, when main thread wants ask windows update calls draw function follows:

void subapplicationmanager::draw() {    // 0 number of applications has finished draw counter   {     boost::lock_guard<boost::mutex> lock(subapplication::draw_mutex);     subapplication::num_draws = 0;   }    // draw sub applications.   (size_t = 0; < m_subapplications.size(); i++)     m_subapplications[i].signal_draw();    // wait until sub applications finish drawing.   while (true){     boost::lock_guard<boost::mutex> lock(subapplication::draw_mutex);     std::cout << subapplication::num_draws << std::endl;     if (subapplication::num_draws >= m_subapplications.size()) break;   }  } 

the draw function signals other thread new task received.

void subapplication::signal_draw() {    task = task::task_draw;   {     boost::lock_guard<boost::mutex> lock(task_received_mutex);     task_received = true;   }   task_start_condition.notify_all();  } 

the body of other thread follows. waits task arrive , start process:

void subapplication::thread() {    clock_t start_time, last_update;   start_time = last_update = clock();    //! creates sub application   init();    while (!done)                                                       // loop runs while done=false   {       // draw scene.  watch esc key , quit messages drawglscene()       if (active)                                       // program active?       {         // wait here, until update/draw command received.         boost::unique_lock<boost::mutex> start_lock(task_start_mutex);         while (!task_received){           task_start_condition.wait(start_lock);         }          // task received set false, next loop.         {           boost::lock_guard<boost::mutex> lock(task_received_mutex);           task_received = false;         }          clock_t frame_start_time = clock();          switch (task){         case task_update:           update();           break;          case task_draw:           draw();           swapbuffers();           break;          case task_create:           create();           break;          default:           break;         }          clock_t frame_end_time = clock();         double task_time = static_cast<float>(frame_end_time - frame_start_time) / clocks_per_sec;        }   } } 

the problem if run code is, never runs other thread task = task::task_draw; if add std::cout << "draw\n"; beginning of subapplication::draw(), work should. looking reason happening , usual way fix it?

boost::lock_guard<boost::mutex> lock(task_received_mutex); task_received = true; 

okay, task_received_mutex protects task_received.

    boost::unique_lock<boost::mutex> start_lock(task_start_mutex);     while (!task_received){       task_start_condition.wait(start_lock);     } 

oops, we're reading task_received without holding mutex protects it. prevents race 1 thread reads task_received while thread modifying it? lead deadlock.

also, have code claims "wait until sub applications finish drawing" there's no call wait function. spins rather waiting, awful.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -