我想隨時從外部暫停和恢復執行緒(不是在某些斷點處,因此wait不會notify作業)。
例如,我們在 中創建一個執行緒foo(),然后它繼續運行。(Thread可以是任何類似于的執行緒類std::thread)
void A::foo() {
this->th = Thread([]{
// This thread runs a time-consuming job with many steps
// I hope to pause and resume it at any time outside ths thread (e.g. press a button)
});
}
我需要暫停和恢復執行緒外的執行緒,也許是通過呼叫這樣的方法......
void A::bar() {
this->th->pause();
cout << "The thread is paused now" << endl;
}
void A::baz() {
this->th->resume();
cout << "The thread is resumed now" << endl;
}
如何在 C 中實作它?
uj5u.com熱心網友回復:
@freakish 說它可以用 pthread 和信號來完成,但沒有可移植的方式。
在 Windows 中,我剛剛發現SuspendThread(t.native_handle())并且ResumeThread(t.native_handle())(其中ttype std::thread)可用。這些將解決我的問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/488417.html
上一篇:ForkPoolTask??.join()ConcurrentModificationException
下一篇:Python理解執行緒和競爭條件
