Boost logo

Boost :

From: Roland Richter (roland_at_[hidden])
Date: 2003-05-08 06:05:13


Dear all,

  I'm new with Boost.Threads; I've just worked with
  Java Threads so far.

  One feature of the Java language is the "synchronized"
  keyword - to make variables, methods, code blocks etc.
  thread-safe. So, when I first came into the situation
  that I needed threads in C++ as well, I thought of a
  way how to reflect that feature into C++.

  It seems to be easy to synchronize variables - see the
  very minimalistic draft below. But what about synchronized
  class methods etc.?

  Is it worth to go further into that direction?

  I mean, the Boost.Thread library seems to be designed with
  safety in mind, but is still a little bit low-level.

  Are there any efforts to enhance the library further?

- Roland

template<class T> class synchronized
{
public:
   synchronized( T theValue = T() )
     : value( theValue )
   {}

   void set( const T& theValue )
   {
     boost::mutex::scoped_lock lock( mux );
     value = theValue;
   }

   T get() const
   {
     boost::mutex::scoped_lock lock( mux );
     return value;
   }

private:
   T value;
   mutable boost::mutex mux;
};


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