Boost logo

Boost Users :

From: Adam Nielsen (adam.nielsen_at_[hidden])
Date: 2008-07-11 02:12:45


Hi all,

I'm having some trouble getting my head around Boost mutexes and how
they work.

Basically I have a handful of objects, and they all use a shared
resource (a C-style function.) The function isn't thread-safe, so I
want to protect it to ensure only one thread at a time can call it.

My idea was to create a mutex for this (lock before the call, unlock
after it), but I'm having trouble working out how to share the mutex
between all the objects. The boost::mutex object seems like what I
want, but it's non-copyable so I can't share it among multiple objects.
  I tried passing around references to the mutex instead, but this just
triggers an assertion failure when I try to lock it.

Should I be using boost::mutex for this? How would you normally
serialise access among different objects like this? I'm finding the
examples somewhat tricky to work from, as most of them use global
variables and C-style functions for creating threads, which doesn't
really fit with the C++ model of my program - I could just make the
mutex a global variable but it doesn't seem like the cleanest solution
to me.

Any pointers would be much appreciated.

Thanks,
Adam.


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