LoginSignup
1
1

More than 5 years have passed since last update.

std::call_once on Ubuntu

Posted at

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

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1