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(); }
source share