Hi,
I'm learning boost::thread from a tutorial. This is the code from that tutorial:
This code ran then crashed at the end:Code:#include <boost/thread.hpp>
#include <boost/date_time.hpp>
#include <iostream>
boost::mutex mutex;
void wait(int seconds)
{
boost::this_thread::sleep(boost::posix_time::seconds(seconds));
}
void thread()
{
try
{
for( int i = 0; i < 5; i++)
{
wait(1);
boost::lock_guard<boost::mutex> lock(mutex);
std::cout<<"Thread "
<< boost::this_thread::get_id()
<< ": " << i <<std::endl;
mutex.unlock();
}
}
catch(boost::thread_interrupted&)
{
std::cout<<"Thread Interrupted" << std::endl;
}
}
int main()
{
boost::thread t1(thread);
boost::thread t2(thread);
t1.join();
t2.join();
return 0;
}
why comes this crash the program? If i use boost::mutex::lock() and unlock() wouldn't crash the program, it won't crash.Quote:
Thread 0x12c22a0: 0
Thread 0x12c2010: 0
Thread 0x12c22a0: 1
Thread 0x12c2010: 1
Thread 0x12c22a0: 2
Thread 0x12c2010: 2
Thread 0x12c22a0: 3
Thread 0x12c2010: 3
Thread 0x12c22a0: 4
Thread 0x12c2010: 4
/usr/include/boost/thread/pthread/mutex.hpp:45: boost::mutex::~mutex(): Assertion `!pthread_mutex_destroy(&m)' failed.