views:

126

answers:

2

I have seen lots of kinds of interface to multithreading and locks.

These make me feel frustrating,

Some of them include 2 different classes like the sample below, while

others have only one class and the acquire() can implement the wait function.

My questions are: Why we design locks like this in object oriented programming?

How to manipulate such objects?

class Lock
{
public:
   Lock();
   ~Lock();

    // Acquire the lock.
   void
   acquire()
   { this->lock_->acquire(); }

    // Release the lock.
   void
   release()
   { this->lock_->release(); }

   private:
   // This class can not be copied.
   Lock(const Lock&);
   Lock& operator=(const Lock&);

   friend class Condvar;
   Lock_impl*
   get_impl() const
   { return this->lock_; }

   Lock_impl* lock_;
};

class Condvar
{
 public:
  Condvar(Lock& lock);
  ~Condvar();
  // Wait for the condition variable to be signalled.  This should
  // only be called when the lock is held.
  void
  wait()
  { this->condvar_->wait(this->lock_.get_impl()); }

  // Signal the condition variable--wake up at least one thread
  // waiting on the condition variable.  This should only be called
  // when the lock is held.
  void
  signal()
  { this->condvar_->signal(); }

  // Broadcast the condition variable--wake up all threads waiting on
  // the condition variable.  This should only be called when the lock
  // is held.
  void
  broadcast()
  { this->condvar_->broadcast(); }

  private:
  // This class can not be copied.
  Condvar(const Condvar&);
  Condvar& operator=(const Condvar&);

  Lock& lock_;
  Condvar_impl* condvar_;
};
+1  A: 

The purpose of locks is to prevent two different processing threads from modifying the same memory location at the same time.

When a thread locks an area of code, and a second thread enters the same area of code, the second thread will wait for the first thread to release its lock before executing the code.

Robert Harvey
+1  A: 

The above is a lock and a condition variable.
These are two unique concepts:

A lock is just a single atomic lock on or off.

A condition variable (is much harder to use correctly) and requires a lock to implement correctly but maintains a state (basically a count).

For information about "Condition Variables" see:
http://en.wikipedia.org/wiki/Monitor_(synchronization)

Basically condition variables are low level primitives used to create "Monitor" (aka Monitor Regions). Monitors are regions of code designed to be used by multiple threads (but usually a controlled number (in simple cases one)) but still be multi-thread safe.

The following provides a good example of using a 'Condition Variable'.
http://stackoverflow.com/questions/206857/how-to-implement-blocking-read-using-posix-threads/206926#206926

Basically 2 threads are allowed into to the Monitor area. A thread is reading from a vector while a second threads is witting from the vector. The 'Monitor' controls the interaction between the 2 threads. Though the same effect can be achieved using just locks it is much/much/much harder to do correctly.

Martin York