私はstd :: conditional_variableを使用して、さまざまなクリティカルセクションのフローを制御するマルチスレッドプログラムの信号をタイミング調整しています。プログラムは動作しますが、終了時には、std :: conditional_variable :: wait()でまだ待機しているスレッドの破壊を避けるために、述語(kill_ == true)を使用するように強制されています。私は待っているすべてのスレッドを破棄する適切な方法、アドバイスを求められたかどうかわからない。私は待機中のスレッドの破壊を処理する方法メインプログラムの終了時にstd :: conditional_variableで待機しているスレッドを破棄する適切な方法
class timer
{
// ...
timer(std::shared_ptr<parent_object> parent,const bool& kill)
:parent_(parent),kill_(kill){}
private:
std::condition_variable cv_command_flow_;
std::mutex mu_flow_;
const bool& kill_;
std::shared_ptr<parent_object> parent_;
};
void timer::section()
{
auto delay = get_next_delay();
std::unique_lock<std::mutex> lock(mu_flow_);
std::cv_command_flow_.wait_until(lock,delay,[] { return kill_ == true; });
if(kill_) return;
parent_->trigger();
std::cv_command_exec_.notify_all();
}
['notify_all'](http://www.cplusplus.com/reference/condition_variable/condition_variable/notify_all/)を試しましたか? – iammilind
用語の問題:スレッドを終了するか、スレッドを最後まで実行しますか?あなたは「糸の破壊」によって何を考えますか? –