LoginSignup
2
2

More than 5 years have passed since last update.

Raspbianで最適化フラグを渡すと std::thread 動かない

Last updated at Posted at 2016-07-08

少々ハマったので情報共有します。

C++11 のスレッドコードを、ラズパイ2でビルドしたら、動きませんでした。
原因は、ラズパイ2向けのコンパイルフラグ -march=armv7-a (-mcpu=cortex-a7 も同じ)。

OS は 2016/5/27 リリースの Raspbian を使ってます。 OS /基本ライブラリ系のバグなのかなぁ。

サンプルコード

問題点を含んだサンプルコード。なるべく最小にしてます。

test.cpp
#include <iostream>
#include <thread>
#include <chrono>


void thread_main(void)
{
    std::this_thread::sleep_for(std::chrono::seconds(5));
}


int main (void)
{
    std::thread t(thread_main);

    std::cout << "main waiting thread_main to finish" << std::endl;

    if (t.joinable()) {
        t.join();
        std::cout << "thread_main finished" << std::endl;
    }

    return 0;
}

動く場合

$ g++ -std=c++11 -pthread test.cpp

実行してみると、こんな感じに(ソースコードのまんま)。

$ ./a.out
main waiting thread_main to finish
thread_main finished

動かない場合

$ g++ -march=armv7-a -std=c++11 -pthread test.cpp

実行してみると・・・

$ ./a.out
pure virtual method called
terminate called without an active exception
Aborted

!! pure virtual method called !!

そういう訳で、スレッド使いたいときは、-march=armv7-a を渡すのは止めましょう。
以上です。

参考情報

ラズパイ2,3に最適なコンパイルフラグ
- https://www.raspberrypi.org/forums/viewtopic.php?f=33&t=144115

同じ問題に直面していそうな StackOverflow の記事:
- http://stackoverflow.com/questions/35929416/c-application-with-threads

2
2
4

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
2
2