かなり単純なスレッド化されたアプリケーションを作成しようとしていますが、boost のスレッド ライブラリは初めてです。私が取り組んでいる簡単なテストプログラムは次のとおりです。
#include <iostream>
#include <boost/thread.hpp>
int result = 0;
boost::mutex result_mutex;
boost::thread_group g;
void threaded_function(int i)
{
for(; i < 100000; ++i) {}
{
boost::mutex::scoped_lock lock(result_mutex);
result += i;
}
}
int main(int argc, char* argv[])
{
using namespace std;
// launch three threads
boost::thread t1(threaded_function, 10);
boost::thread t2(threaded_function, 10);
boost::thread t3(threaded_function, 10);
g.add_thread(&t1);
g.add_thread(&t2);
g.add_thread(&t3);
// wait for them
g.join_all();
cout << result << endl;
return 0;
}
ただし、このプログラムをコンパイルして実行すると、次の出力が得られます
$ ./test
300000
test: pthread_mutex_lock.c:87: __pthread_mutex_lock: Assertion `mutex->__data.__owner == 0' failed.
Aborted
明らかに、結果は正しいのですが、特に、本質的に同じ構造を持つ実際のプログラムが join_all() ポイントでスタックしているため、このエラー メッセージが心配です。誰かが私に何が起こっているのか説明できますか? これを行うためのより良い方法はありますか?つまり、多数のスレッドを起動し、それらを外部コンテナに保存し、プログラムを続行する前にすべてのスレッドが完了するのを待ちますか?
ご協力いただきありがとうございます。