ブースト http://www.boost.org/doc/libs/1_31_0/libs/thread/example/thread_group.cppからサンプルプログラムをコピーするだけです
#include <boost/thread/thread.hpp>
#include <iostream>
int shared_counter = 0;
boost::mutex mutex;
void increment_count()
{
boost::mutex::scoped_lock lock(mutex);
std::cout << "count = " << ++shared_counter << std::endl;
}
int main(int argc, char* argv[])
{
boost::thread_group threads;
for (int i = 0; i < 10; ++i)
threads.create_thread(&increment_count);
threads.join_all();
}
そしてそれを実行します
valgrind --tool=drd thread_group
そして、以下に示す多くのエラーが報告されます。
==1454== Thread 2:
==1454== Conflicting load by thread 2 at 0x100843650 size 4
==1454== at 0x1006FD94E: putc (in /usr/lib/libSystem.B.dylib)
==1454== by 0x1005C0A18: std::ostream::put(char) (in /usr/lib/libstdc++.6.0.9.dylib)
==1454== by 0x1005C0AC6: std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&) (in /usr/lib/libstdc++.6.0.9.dylib)
==1454== by 0x10001BD32: increment_count() (libfbj.cpp:23)
==1454== by 0x10001CCD6: boost::detail::thread_data<void (*)()>::run() (thread.hpp:62)
==1454== by 0x10053DF17: thread_proxy (in /opt/local/lib/libboost_thread.dylib)
==1454== by 0x1001005A5: vgDrd_thread_wrapper (drd_pthread_intercepts.c:341)
==1454== by 0x1006AEFD5: _pthread_start (in /usr/lib/libSystem.B.dylib)
==1454== by 0x1006AEE88: thread_start (in /usr/lib/libSystem.B.dylib)
==1454== Allocation context: Data section of /usr/lib/libSystem.B.dylib
==1454== Other segment start (thread 1)
==1454== at 0x100102286: pthread_mutex_unlock (drd_pthread_intercepts.c:665)
==1454== by 0x10001EBDE: boost::mutex::unlock() (mutex.hpp:72)
==1454== by 0x10001EE5C: boost::unique_lock<boost::mutex>::~unique_lock() (locks.hpp:403)
==1454== by 0x10001F097: boost::shared_mutex::lock() (shared_mutex.hpp:137)
==1454== by 0x10001F10E: boost::lock_guard<boost::shared_mutex>::lock_guard(boost::shared_mutex&) (locks.hpp:257)
==1454== by 0x10001F751: boost::thread* boost::thread_group::create_thread<void (*)()>(void (*)()) (thread_group.hpp:41)
==1454== by 0x10001BC3F: main (libfbj.cpp:30)
==1454== Other segment end (thread 1)
==1454== at 0x1006AEE52: __bsdthread_create (in /usr/lib/libSystem.B.dylib)
==1454== by 0x100106D8E: pthread_create (drd_pthread_intercepts.c:457)
==1454== by 0x10053D78A: boost::thread::start_thread() (in /opt/local/lib/libboost_thread.dylib)
==1454== by 0x10001F31A: boost::thread::thread<void (*)()>(void (*)(), boost::disable_if<boost::is_convertible<void (*&)(), boost::detail::thread_move_t<void (*)()> >, boost::thread::dummy*>::type) (thread.hpp:205)
==1454== by 0x10001F771: boost::thread* boost::thread_group::create_thread<void (*)()>(void (*)()) (thread_group.hpp:42)
==1454== by 0x10001BC3F: main (libfbj.cpp:30)
...
==1454== ERROR SUMMARY: 49 errors from 6 contexts (suppressed: 215 from 39)
私はMac OS X 10.6.8、Xcode 3.2.4 64ビット、gcc 4.2.1、プロジェクト設定「Other C++ FLAGs」-pthread、「sudo port install valgrind」によってインストールされたvalgrind 3.7.0、およびブースト1.49を使用していますw /
sudo ./bjam variant=debug define=BOOST_LOG_USE_CHAR architecture=combined address-model=32_64 link=shared,static threading=multi install
私もpthreadを試しました。ここにプログラムがあります
#include <pthread.h>
#include <assert.h>
#include <iostream>
#define NUM_THREADS 5
pthread_mutex_t m_mutex;
void *TaskCode(void *argument)
{
pthread_mutex_lock(&m_mutex);
std::cout << "Hello World! It's me, thread " << *((int *) argument) << std::endl;
pthread_mutex_unlock(&m_mutex);
return NULL;
}
int main(void)
{
pthread_mutex_init(&m_mutex,0);
pthread_t threads[NUM_THREADS];
int thread_args[NUM_THREADS];
int rc, i;
// create all threads
for (i=0; i<NUM_THREADS; ++i) {
thread_args[i] = i;
rc = pthread_create(&threads[i], NULL, TaskCode, (void *) &thread_args[i]);
assert(0 == rc);
}
// wait for all threads to complete
for (i=0; i<NUM_THREADS; ++i) {
rc = pthread_join(threads[i], NULL);
assert(0 == rc);
}
pthread_mutex_destroy(&m_mutex);
return 0;
}
次に、「g++ -pthread -lpthread threaded_group.cpp -o threaded_group」および「valgrind --tool=drd --dsymutil=yes ./threaded_group」でエラーが発生します。
==71472== Thread 2:
==71472== Conflicting load by thread 2 at 0x1001fd650 size 4
==71472== at 0x1000933DB: fwrite (in /usr/lib/libSystem.B.dylib)
==71472== by 0x1002ADAE3: void std::__ostream_write<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) (in /usr/lib/libstdc++.6.0.9.dylib)
==71472== by 0x1002ABB97: std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) (in /usr/lib/libstdc++.6.0.9.dylib)
==71472== by 0x1002ABC57: std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*) (in /usr/lib/libstdc++.6.0.9.dylib)
==71472== by 0x100000C46: TaskCode(void*) (in ./threaded_group)
==71472== by 0x1000145A5: vgDrd_thread_wrapper (drd_pthread_intercepts.c:341)
==71472== by 0x100068FD5: _pthread_start (in /usr/lib/libSystem.B.dylib)
==71472== by 0x100068E88: thread_start (in /usr/lib/libSystem.B.dylib)
==71472== Allocation context: Data section of /usr/lib/libSystem.B.dylib
==71472== Other segment start (thread 1)
==71472== at 0x10001AE58: pthread_create (drd_pthread_intercepts.c:468)
==71472== by 0x100000B70: main (in ./threaded_group)
==71472== Other segment end (thread 1)
==71472== at 0x100068E52: __bsdthread_create (in /usr/lib/libSystem.B.dylib)
==71472== by 0x10001AD8E: pthread_create (drd_pthread_intercepts.c:457)
==71472== by 0x100000B70: main (in ./threaded_group)
==71472== Other segment start (thread 1)
==71472== at 0x10001AE58: pthread_create (drd_pthread_intercepts.c:468)
==71472== by 0x100000B70: main (in ./threaded_group)
==71472== Other segment end (thread 1)
==71472== at 0x100068E52: __bsdthread_create (in /usr/lib/libSystem.B.dylib)
==71472== by 0x10001AD8E: pthread_create (drd_pthread_intercepts.c:457)
==71472== by 0x100000B70: main (in ./threaded_group)
==71472== Other segment start (thread 1)
==71472== at 0x10001AE58: pthread_create (drd_pthread_intercepts.c:468)
==71472== by 0x100000B70: main (in ./threaded_group)
==71472== Other segment end (thread 1)
==71472== at 0x100068E52: __bsdthread_create (in /usr/lib/libSystem.B.dylib)
==71472== by 0x10001AD8E: pthread_create (drd_pthread_intercepts.c:457)
==71472== by 0x100000B70: main (in ./threaded_group)
...
==71472== ERROR SUMMARY: 11 errors from 6 contexts (suppressed: 70 from 29)
誰が何が悪いのか教えてもらえますか?