Ubunbu(16.04)上で、ごく単純なstd::call_once
を呼ぶコードをコンパイル・リンクして実行すると、
#include <mutex>
#include <iostream>
int main(void) {
std::once_flag flag;
std::call_once(flag, []() {
std::cout << "hello world\n";
});
}
$ g++ -std=c++11 a.cpp
$ ./a.out
terminate called after throwing an instance of 'std::system_error'
what(): Unknown error -1
Aborted (core dumped)
なぜか実行時に例外が投げられます。しかし-pthread
を付けると、
$ g++ -std=c++11 a.cpp -pthread
$ ./a.out
hello world
上手く動きます。MacOSでは-pthread
なしでも問題ありませんでした。
参考:
http://stackoverflow.com/questions/15311469/throwing-an-exception-from-stdcall-once
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60662
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58929