Boost logo

Boost :

From: Matt Hurd (matt_at_[hidden])
Date: 2002-11-05 23:46:19


Hi,

Thanks for all the work you guys do. Gives life a little boost...

I'm getting a some untidy memory faults on a simple thread program using the
elegant boost thread library from the thread_development tag with VC7 on
process exit.

Might be related to this link warning...

Creating library Release/boost_thread_play.lib and object
Release/boost_thread_play.exp
tss.obj : warning LNK4217: locally defined symbol _on_thread_exit imported
in function "class std::vector<struct std::pair<int,void *>,class
std::allocator<struct std::pair<int,void *> > > * __cdecl `anonymous
namespace'::get_slots(bool)"
(?get_slots@?A0x898b778b@@YAPAV?$vector_at_U?$pair_at_HPAX@std@@V?$allocator_at_U?$pa
ir_at_HPAX@std@@@2@@std@@_N_at_Z)

This is from a rw_mutex.html example. The html is unfinished. I took the
example code at the bottom and cleaned up a few typos. You might like to
use it to replace the example in the code, see below.

The existing win32 mutexes use critical sections. I see there is some
support for shared memory in thread development. Would love to see some
policy perhaps for win32 mutexes so it would be possible to use a cross
process mutex, such as the win32 mutex object. This would be especially
useful for win32. Could see a whole bunch on interesting policy
possibilites for this library but I'm just a simple fellow...

Regards,

matt.

____________________________________________

#include <boost/thread/rw_mutex.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/detail/rw_lock.hpp>
#include <iostream>

boost::mutex io_mutex; // The iostreams are not guaranteed to be
thread-safe!

class counter
{
public:
    counter() : rwm(boost::sp_writer_priority) , count(0) { }

    int increment() {
        boost::rw_mutex::scoped_rw_lock scoped_rw_lock(rwm);
        return ++count;
    }

    int get() {
        boost::rw_mutex::scoped_rw_lock scoped_rw_lock(rwm,
boost::SHARED_LOCK);
            return count;
    }

private:
    boost::rw_mutex rwm;
    int count;
};

counter c;

void change_count()
{
    int i = c.increment();
    boost::mutex::scoped_lock scoped_lock(io_mutex);
    std::cout << "write_count == " << i << std::endl;
}

void get_count()
{
    int i = c.get();
    boost::mutex::scoped_lock scoped_lock(io_mutex);
    std::cout << "read_count == " << i << std::endl;
}

int main(int argc, char* argv[])
{
    const int num_threads = 5;
    boost::thread_group thrds;
    for (int i=0; i < num_threads; ++i)
    {
        thrds.create_thread(&change_count);
        thrds.create_thread(&get_count);
    }

    thrds.join_all();

    return 0;
}


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