備忘です。
FreeBSD 9.1-RELEASEでclang++でC++11のスレッドを含むプログラムのビルド方法。
以下のソースコードを
hello_thread.cpp
#include <iostream>
#include <chrono>
#include <thread>
void f()
{
std::chrono::milliseconds ms(500);
for (int i = 0; i < 10; i++) {
std::cout << __func__ << "()" << std::endl;
std::this_thread::sleep_for(ms);
}
}
int main(void)
{
std::thread thr(f);
std::chrono::milliseconds ms(250);
for (int i = 0; i < 10; i++) {
std::cout << __func__ << "()" << std::endl;
std::this_thread::sleep_for(ms);
}
thr.join();
return 0;
}
を以下のコマンドでビルド。
clang++ -std=c++11 -stdlib=libc++ -o hello_thread hello_thread.cpp -lpthread
pthreadライブラリのリンクは必須みたい。
↓実行結果はこんな感じ
> ./hello_thread
main()
f()
main()
f()
main()
main()
f()
main()
main()
f()
main()
main()
f()
main()
main()
f()
f()
f()
f()
f()
>