こんにちは私はワーカースレッドを終了するための最高の平和な方法を見つけることを試みています。私は、次のコードを持っている:ワーカースレッドを終了する適切な方法
class test{
public:
test() {}
~test() {}
std::atomic<bool> worker_done;
int a;
void pr() {
while (true) {
if (worker_done) {
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(500));
printf("%d \n", a++);
}
}
std::thread* m_acqThread;
void continuous() {
m_acqThread = new std::thread(&test::pr, this);
}
void stopThread(){
if (m_acqThread) {
if (m_acqThread->joinable())
m_acqThread->join();
delete m_acqThread;
m_acqThread = nullptr;
}
}
};
int main(){
test t;
t.continuous();
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
t.worker_done = true;
t.stopThread();
std::string str;
std::cin.clear();
getline(std::cin, str);
return 0;
は真であると「worker_done」を設定する以外に終了する労働者のスレッドに通知する良い方法はありますか?あなたが持っているもの
おかげ
"...最高の平和な方法..."暴力的な方法はありますか? –
手動ポインタトラッキングの代わりに 'unique_ptr'を使用してください。 – GManNickG
非敵対的な殺害は、私が彼/彼女が – ApolloSoftware