Boost logo

Boost Users :

From: Christian Henning (chhenning_at_[hidden])
Date: 2005-08-30 18:06:26

> What if I will access the eState member from the function (running in another thread) while this
> thread will write it?

Make the access to eState threadsafe, probably with an read_write_mutex

an example:

class ThreadState
  enum teThreadState { BLOCKED=0, RUNNING };

  teThreadState getState() throw() { scoped_read_lock oLock(oMutex);
return _eState; }
  void setState( teThreadState eState ) { scoped_write_lock oLock(
oMutex ); _eState = eState; }


  teThreadState _eState;
  boost::read_write_mutex oMutex;

> Is it possible to try to lock an already locked mutex receive an error or exception and know that
> if the mutex is not lockable another thread is still running, otherwise another thread is done...
> Would be that an option? If yes, I can't get it to work.

Just encapsulate a Mutex in a separate class and everytime you wont to
lock it, check if the state is locked or not. But be careful and make
it as threadsafe as possible.

The boost thread lib is a very low level library, to ensure
portability. That's the reason why there are so many basic
functionality is missing. But it's working and does what it's supposed


Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at