Boost.Thread は、ロックフリーについての保証を提供しませんtimed_join()
。ただし、実装は常に変更される可能性があります。
- Boost.Thread は、pthreads のミューテックスを取得し、条件変数で時間指定待機を実行します。
WaitForMultipleObjects
Windows のBoost.Thread 呼び出し。そのドキュメントは、常にすぐに戻ることを示しています。ただし、基盤となる OS の実装がロックフリーかどうかはわかりません。
別の方法として、アトミック操作の使用を検討してください。Boost 1.52 は現在、パブリック アトミック ライブラリを提供していませんが、Boost.Smart_Ptr と Boost.Interprocess の両方が詳細名前空間内にアトミック整数を持っています。ただし、これらはどちらもロックフリーの実装を保証するものではなく、Boost.Smart_Ptr の構成の 1 つが でロックされpthread mutex
ます。したがって、コンパイラとシステムのドキュメントを調べて、ロックのない実装を特定する必要がある場合があります。
それにもかかわらず、ここに を使用した小さな例がありますboost::detail::atomic_count
:
#include <boost/chrono.pp>
#include <boost/detail/atomic_count.hpp>
#include <boost/thread.hpp>
// Use RAII to perform cleanup.
struct count_guard
{
count_guard(boost::detail::atomic_count& count) : count_(count) {}
~count_guard() { --count_; }
boost::detail::atomic_count& count_;
};
void thread_main(boost::detail::atomic_count& count)
{
// Place the guard on the stack. When the thread exits through either normal
// means or the stack unwinding from an exception, the atomic count will be
// decremented.
count_guard decrement_on_exit(count);
boost::this_thread::sleep_for(boost::chrono::seconds(5));
}
int main()
{
boost::detail::atomic_count count(1);
boost::thread t(thread_main, boost::ref(count));
// Check the count to determine if the thread has exited.
while (0 != count)
{
std::cout << "Sleeping for 2 seconds." << std::endl;
boost::this_thread::sleep_for(boost::chrono::seconds(2));
}
}
この場合、拡張機能はRAIIat_thread_exit()
を使用する代わりに使用できます。