まず第一に「適切」です。なぜなら、クラスからスレッドを起動するのは悪い習慣だと人々が言うと思うからです。:)だから、デストラクタで無限スレッドを停止する最良の方法は何だろうと思っています。フラグ shutdown が設定されているときに、スレッドが try で呼び出して例外をスローする関数をラッピングしますか? 古き良きint / enum?良い新しい std::atomic int? 他の何か?今私は使用します:
//in destructor I call terminate member func
void terminate()
{
currentStatus=terminating;
std::cout<<"trying to kill"<<std::endl;
while (currentStatus!=terminated)
std::this_thread::yield();
std::cout<<"MISSION ACOMPLISHED"<<std::endl;
}
スレッドが実行する関数は次のとおりです。
while (currentStatus==active)
{
//...
}
currentStatus=terminated;
currentStatus は列挙型です:
enum status{
active,
terminating,
terminated
};