私は簡単なプログラムを持っています。
int main()
{
std::atomic<bool> b = true;
ConcurrentQueue<std::string> queue;
std::thread thread( [&]{
while ( b ) {
auto str = queue.wait_and_pop();
std::cout << *str;
}
});
b = false;
queue.push( "end" );
thread.join();
}
ConcurrentQueue<T>
はスレッド セーフ キューの私自身の実装であり、wait_and_pop
は を使用するブロッキング操作ですstd::condition_variable
。
このプログラムは "end" を正常に出力して終了します。ここでは問題ありません。b
(開始時に falsethread
であり、すぐに終了するバグがありますが、ここでは関係ありません)
しかし、これらすべてをクラスにラップすると
class object {
public:
object() {
b = true;
thread = std::thread( [this]{
while ( b ) {
auto str = queue.wait_and_pop();
std::cout << *str;
}
});
}
~object() {
b = false;
queue.push( "end" );
thread.join();
}
private:
std::atomic<bool> b;
std::thread thread;
ConcurrentQueue<std::string> queue;
};
そして、次のような関数静的変数を持っています
object & func() {
static object o;
return o;
}
そしてメイン
int main() {
object & o = func();
}
現在、プログラムは「end」を出力し、o
at lineのデストラクタでスタックしていthread.join()
ます。
これをclangでテストしましたが、問題ありません。これは VC11 でのみ発生するようです。何故ですか?