The SimpleCxxLib package


#include "thread.h"

class Lock

This class represents a simple lock used to control concurrency. The usual strategy for using locks is to use the synchronized macro described later in this interface.
Constructor
Lock() Initializes a lock, which is initially in the unlocked state.
Methods
signal() Signals all threads waiting on the lock so that they wake up and recheck the corresponding condition.
wait() Waits for some other thread to call signal on this lock.
Statement
synchronized(lock) Defines a critical section protected by the specified lock.

Constructor detail


Lock();
Initializes a lock, which is initially in the unlocked state.

Usage:

Lock lock;

Method detail


void wait();
Waits for some other thread to call signal on this lock. This call requires that the lock be held by the calling thread. The effect of the wait method is to release the lock and then wait until the desired signal operation occurs, at which point the lock is reacquired and control returns from the wait call. The wait method is typically used inside a critical section containing a while loop to check for a specific condition. The standard paradigm for using the waitThread function looks like this:

   synchronized (lock) {
      while (conditional test) {
         lock.wait();
      }
      ... code to manipulate the locked resource ...
   }

Usage:

lock.wait();

void signal();
Signals all threads waiting on the lock so that they wake up and recheck the corresponding condition.

Usage:

lock.signal();

Statement detail


synchronized (lock) ...
Defines a critical section protected by the specified lock. The general strategy for using this facility is shown in the following paradigmatic pattern:

   synchronized (lock) {
      ... statements in the critical section ...
   }