Boost logo

Boost :

From: Matt Hurd (matt.hurd_at_[hidden])
Date: 2005-08-30 18:44:59


>On 31/08/05, John Maddock <john_at_[hidden]> wrote:

> Matt, can you distil this test case a little more please:
>
> * Get rid of the macros so we can read the code :-)
> * Don't use Boost.Test for the tests, it's *not* thread safe, and bad things
> may be happening in there.
> * Don't declare variables as function-static: it's not thread safe. I'm not
> sure if this is an issue or not from reading the code, with those macros in
> place it's hard to see which bits are getting called by multiple threads.

John,

Hopefully this is a little more readable. My set up is gcc 3.3.5 on
Suse 9.3 pro x86_64 with dual opterons.

Hope this helps,

matt.
matt_at_[hidden]
___________________________

#include <iostream>

#include <boost/thread/thread.hpp>
#include <boost/thread/read_write_mutex.hpp>

using namespace boost;

// these definitions deadlock
read_write_mutex guard( read_write_scheduling_policy::writer_priority );
typedef read_write_mutex::scoped_write_lock test_lock_type;

// these definitions work as expected
// mutex guard;
// typedef mutex::scoped_lock test_lock_type;

long global_count = 0;
long loop_count_for_test = 100000;

void do_work()
{
    for (long i=0; i< loop_count_for_test; ++i )
    {
        test_lock_type lk( guard );
        ++global_count;
    }
}

int main()
{
    
    thread_group pool;
    int number_of_threads = 100;

    for (int i=0; i < number_of_threads; ++i)
    {
        pool.create_thread( do_work );
    }
         
    pool.join_all();
    
    //result check
    std::cout << "We should get here without a deadlock\n";
    std::cout << "global count = " << global_count << "\n";
    std::cout << "global count should be = "
        << loop_count_for_test * number_of_threads
        << "\n";
       
    return 0;
}


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