How to block and wake up the flow of boost?

how can i block the boost thread and wake it from another thread? The thread does some work, if the work is completed, it should block or sleep, if the new work is ready, the main thread should weaken the work thread. I tried this with the lock read in boost ipc message_queue, but this was not a solution for performers.

Something like that:

void thread() { uint8_t ret=0; for(;;) //working loop { ret=doWork(); if(ret==WORK_COMPLETE) { BlockOrSleep(); } } } 

With pthreads, I can block the semaphore, but it is platform independent.

+4
source share
1 answer

One solution to this problem is the Variable Condition , which, as the name implies, is a way to wait in the stream until the condition is reached. This requires some mutual cooperation between threads.

From the example in the documentation I linked above:

Theme 1:

 boost::condition_variable cond; boost::mutex mut; bool data_ready; void process_data(); void wait_for_data_to_process() { boost::unique_lock<boost::mutex> lock(mut); while(!data_ready) { cond.wait(lock); } process_data(); } 

Theme 2:

 void retrieve_data(); void prepare_data(); void prepare_data_for_processing() { retrieve_data(); prepare_data(); { boost::lock_guard<boost::mutex> lock(mut); data_ready=true; } cond.notify_one(); } 
+6
source

Source: https://habr.com/ru/post/1493151/


All Articles