LoginSignup
6
6

More than 5 years have passed since last update.

備忘: clang++でC++11のスレッド

Posted at

備忘です。
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()
>
6
6
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
6
6