boost::thread

公開:2004-06-28 06:09
更新:2020-02-15 04:36
カテゴリ:boost,windows,c++

boost::threadを使用しはじめましたが、困ったことが発生しました。
boost::threadライブラリはスタティックライブラリが提供されません。
そのせいか、Cランタイムライブラリをスタティックにリンクしている場合、コンパイル時にboost::threadで使っているautolink.hppがエラーを出します。

fatal error C1189: #error : "Mixing a dll boost library with a static runtime is a really bad idea..."
これは困りました。ライブラリはスタティックライブラリでビルドしないとインストーラに依存するDLLを入れないといけないので面倒(自分が)ですからね。
なぜスタティックライブラリが提供されてないのかgoogleしたところ、ここに書いてありました。

There is no static thread library for win32. Search the archives for
discussions as to why, but basically the cleanup required for TLS
couldn't be implemented unless the library is built as a DLL on win32.

If you don't use TLS, then you could possibly build your own static
thread library from the source, but that isn't an official recommendation.

(I do it, but I create all threads at the start of the application and
they exit at the end, so no cleanup is required while the application is
running (the OS should cleanup resources when the app exits) but you do
this at your own risk)
Win32の場合DLLでないとTLS(スレッドローカルストレージ)が初期化できないためだそうです。TLSを使わないんだったら自力でビルドすればスタティックライブラリとして使用できるようですね。