Boost logo

Boost Users :

From: James Zappia (James.Zappia_at_[hidden])
Date: 2002-12-04 11:47:56


> 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.

>>How is it not working? What do you expect to happen?

What I want to happen is that the main thread keeps trying the lock until
it's freed.

> SOURCE:
>
> #include <iostream>
> #include <boost/thread/thread.hpp>
> #include <boost/thread/exceptions.hpp>
>
> 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);

>>This will force a lock (which means we'll block until the thread releases
>>the lock).

> while ( !lock.try_lock() )

>>Since the above insured the mutex is already locked, this would be a
>>recursive lock which should result in a thread_lock_error exception being
>>thrown. Is this not what's happening for you?

That is what's happening. As stated above, what I would like to happen is
the main thread continuously try the lock until it's freed. What am I doing
wrong?

Thanks for the help!
James

> cout << "main: retrying to lock mutex\n";
>
> cout << "main: joining thread\n";
> thread.join();
>
> cout << "main: thread joined\n";
> return 0;
> }

>>William E. Kempf

Info: <http://www.boost.org>
Wiki: <http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl>
Unsubscribe: <mailto:boost-users-unsubscribe_at_[hidden]>
 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net