Hi,
I'm a newbie to C++ and Boost and I'm trying to port a threaded C program I
have to Boost.Threads. I'm trying to use scoped_try_lock to test a lock on
mutex and it's not working. I'm obviously doing something wrong but I can't
figure it out. Below is the source code. Any help would be greatly
appreciated.
Thanks,
James
SOURCE:
#include <iostream>
#include
#include
using namespace std;
boost::try_mutex mutex;
void thread_test( void )
{
cout << "thread: locking mutex\n";
{
boost::try_mutex::scoped_try_lock lock(mutex);
cout << "thread: sleeping for 10 seconds\n";
sleep(10);
}
cout << "thread: unlocked mutex - goodbye!\n";
return;
}
int main( void )
{
cout << "main: creating thread\n";
boost::thread thread(thread_test);
cout << "main: sleeping for 2 seconds\n";
sleep(2);
cout << "main: trying to lock mutex\n";
boost::try_mutex::scoped_try_lock lock(mutex,true);
while ( !lock.try_lock() )
cout << "main: retrying to lock mutex\n";
cout << "main: joining thread\n";
thread.join();
cout << "main: thread joined\n";
return 0;
}