Boost logo

Boost Users :

From: James Zappia (James.Zappia_at_[hidden])
Date: 2002-12-04 17:34:12


> Thank you, that worked. I see what I was doing wrong.
>
> One thing I don't understand about the alternative method...
>
>> boost::try_mutex::scoped_try_lock lock(mutex); // does a try_lock
>> while (!lock) {
>> cout << "trying...";
>> lock.try_lock();
>> }
>
> How does "!lock" in the "while (!lock) { ... }" statement determine
> whether it's true or false? Does it call a function in the
> scoped_try_lock class? (sorry if that's a lame question...)

>Hmmm... this doesn't seem to be documented very well. I'll have to fix
>that. What it does is invoke operator void*() for a boolean test (this
>mechanism also should be updated to use the safe_bool idiom as well).
>Alternatively, you could call lock.locked() (which also appears to be
>poorly documented). Thanks for pointing out these deficiencies.

>William E. Kempf

Thanks for all you help. I have a few new questions now, though. ;) Again,
I'm a newbie to C++ so this may seem stupid...

I created a boost::thread object and even though it goes out of scope, the
thread continues to run. Also, when passing a class with the operator()
defined to boost::thread, it constructs and destructs the object several
times which seems odd. What I'm basically trying to do is run a thread,
have that thread lock a timed_mutex, then do its job. Then, in main, I try
to lock the same timed_mutex and if I can't get a lock before the time
expires, I need to terminate the thread. Below is the source code and the
output generated. My questions are below, after the output.

SOURCE:
#include <iostream>
#include <boost/thread/xtime.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/exceptions.hpp>

using namespace std;

boost::timed_mutex mutex;

class thread_class
{
public:
        explicit thread_class(int sleep): _sleep(sleep) {};

        void operator()()
        {
                boost::xtime xt;

                cout << "thread: locking mutex\n";
                boost::timed_mutex::scoped_timed_lock lock(mutex,true);

                boost::xtime_get(&xt,boost::TIME_UTC);

                cout << "thread: sleeping for " << _sleep << " seconds\n";

                xt.sec += _sleep;
                boost::thread::sleep(xt);

                cout << "thread: freeing lock -- goodbye\n";
        }

        ~thread_class() { cout << "thread_class destructed\n"; }

private:
        int _sleep;
};

int main( void )
{
        {
                cout << "main: creating thread\n";

                thread_class test(10);
                boost::thread thrd(test);

                cout << "main: sleeping for 2 seconds\n";
                sleep(2);

                cout << "main: trying to lock mutex -- waiting 5
seconds...\n";

                boost::timed_mutex::scoped_timed_lock lock(mutex,false);

                boost::xtime xt;
                boost::xtime_get(&xt,boost::TIME_UTC);
                xt.sec += 5;

                if ( lock.timed_lock(xt) )
                {
                        cout << "obtain lock -- joining thread\n";
                        thrd.join();
                }
                else
                {
                        cout << "failed to obtain lock\n";
                }
        }

        cout << "main: making sure thread is gone\n";
        sleep(10);

        return 0;
}

OUTPUT:
main: creating thread
thread_class destructed
thread_class destructed
thread_class destructed
thread_class destructed
main: sleeping for 2 seconds
thread: locking mutex
thread: sleeping for 10 seconds
main: trying to lock mutex -- waiting 5 seconds...
failed to obtain lock
thread_class destructed
main: making sure thread is gone
thread: freeing lock -- goodbye
thread_class destructed

1) Why are so many thread_class objects constructed/destructed when calling
boost::thread?
2) After the thread object (test) goes out of scope, why does the thread
continue to run? Is it copied when calling boost::thread and run detached
from the main thread?
3) Is there a way I can cancel a thread if going out of scope doesn't stop
it?

Thanks again,
James

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