There is global object boost::unique_lock. One thread locks it. When other thread tries to lock the exception "already owns the mutex" thrown. Why this is happening? I expect the thread to be blocked until other thread will call unlock.
boost::mutex mutex;
boost::unique_lock<boost::mutex> lock(mutex);
static void* scheduller(void* arg)
{
boost::this_thread::sleep(boost::posix_time::seconds(5));
lock.lock();
return 0;
}
static void* usb(void* arg)
{
lock.lock();
boost::this_thread::sleep(boost::posix_time::seconds(20));
return 0;
}
int BEvent_test()
{
lock.unlock();
int a = 0;
boost::thread thread1(usb,&a);
boost::thread thread2(scheduller,&a);
boost::this_thread::sleep(boost::posix_time::seconds(100));
return 0;
}
In BEvent_test function I'm doing unlock because unique lock is locked in constructor. Delays make sure that scheduller thread starts after usb thread. When it tries to do lock() exception is thrown. Why?