Boost logo

Boost :

From: Preston A. Elder (prez_at_[hidden])
Date: 2007-11-13 20:44:46


On Tue, 13 Nov 2007 19:50:04 +0000, Preston A. Elder wrote:

> Ironically, the new interface makes this whole thing harder. Mainly
> because of two reasons:

OK, I take it all back!

Here is my new interruptable_mutex, using the new boost::thread:
http://www.neuromancy.net/fisheye/browse/mantra/trunk/Mantra-I/mantra/utils/interruptable_mutex.h?r=428

It turns out it is much simpler to implement ;)

Here is my test app:

-----------8<---------------------------------------------------------------

#include <boost/thread.hpp>
#include <interruptable_mutex.h>

using namespace boost;

typedef interruptable_mutex test_mutex;
// typedef mutex test_mutex;

test_mutex m;
condition c;
bool stopwait;

void locking_cond()
{
        std::string tid = lexical_cast<std::string>(this_thread::get_id());
        printf("Started thread %s\n", tid.c_str());
        try
        {
                test_mutex::scoped_lock sl(m);
                printf("%s: Lock acquired!\n", tid.c_str());
                while (!stopwait)
                        c.wait(sl);
                printf("%s: Exited condition!\n", tid.c_str());
                sleep(5);
        }
        catch (thread_interrupted &e)
        {
                printf("%s: Lock interrupted!\n", tid.c_str());
        }
        printf("Ended thread %s\n", tid.c_str());
}

void locking_func()
{
        std::string tid = lexical_cast<std::string>(this_thread::get_id());

        printf("Started thread %s\n", tid.c_str());
        try
        {
                test_mutex::scoped_lock sl(m);
                printf("%s: Lock acquired!\n", tid.c_str());
                sleep(5);
        }
        catch (thread_interrupted &e)
        {
                printf("%s: Lock interrupted!\n", tid.c_str());
        }
        printf("Ended thread %s\n", tid.c_str());
}

int main()
{
        stopwait = false;
        thread *t1 = new thread(&locking_cond);
        sleep(1);
        thread *t2 = new thread(&locking_cond);
        sleep(1);

        test_mutex::scoped_lock sl(m);

        printf("Interrupting thread %s\n", boost::lexical_cast<std::string>(t2->get_id()).c_str());
        t2->interrupt();

        sleep(5);
        sl.unlock();
        stopwait = true;
        c.notify_all();

        sleep(15);
        return 0;
}

-----------8<---------------------------------------------------------------

Simply change the typedef of test_mutex to see the difference between a regular
mutex and interruptable_mutex. And change the boost::thread function to change
between the lock and the condition test.

The nice thing about this is it is a template, so any lock type can be used with
this lock. Of course, it has t have a try_lock() and unlock() function.

OH, and in case you want to, I give full permission for this to be included inside
boost and released under the boost software license.

PreZ :)


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk