AMDG On 03/25/2011 12:58 PM, Panagiotis Foteinos wrote:
I am new to boost:threads, so my apologies in advance.
I am getting an assertion failure right before main() exits. Specifically, I get the following error: "*/usr/include/boost/thread/pthread/mutex.hpp:45: boost::mutex::~mutex(): Assertion `!pthread_mutex_destroy(&m)' failed.*"
Below is the code snippet:
*********************************************************************************************************************************** class Test { private: boost::mutex mtx;
public: int counter; Test() { counter = 10; } void DoWork() { for(unsigned int i = 0; i< 1000000; i++) { mtx.try_lock(); counter++; mtx.unlock(); } } };
int main() { unsigned int N = 7; boost::thread* test_threads = new thread_type[N]; for(unsigned int i = 0; i< N; i++) { test_threads[i] = thread_type(boost::lambda::bind<void>(&Test::insert,&object)); }
for(unsigned int i = 0; i< N; i++) { test_threads[i].join(); }
return 1; } ***********************************************************************************************************************************
Can somebody shed some light? Main() exits normally, if I replace mtx.try_lock() with mtx.lock(). Why this behavior?
You have to test the result of try_lock. try_lock doesn't block if the mutex is already locked, thus your code can call unlock on a mutex that isn't locked. In Christ, Steven Watanabe