Top Up Prev Next Bottom Contents Index Search

2.1 Class PtGate

Objects of classes derived from PtGate are used as semaphores to obtain exclusive access to some resource. PtGate is an abstract base class: it specifies certain functionality but does not provide an implementation. Derived classes typically provide the desired semantics for use with a particular threading library. PtGate has three virtual functions that must be implemented by each derived class. The first is a public method:

virtual PtGate* makeNew() const = 0; 
The makeNew method returns a new object of the same class as the object it is called for, which is created on the heap. For example, a hypothetical SunLWPGate object would return a new SunLWPGate. The other two methods are protected. They are:

virtual void lock() = 0; 
virtual void unlock() = 0;
The first call requests access for a resource; the second call releases access. If code in another thread calls lock() on the same PtGate after lock() has already been called on it, the second call will block until the first thread does an unlock() call. Note that two successive calls to lock() on the same PtGate from the same thread will cause that thread to hang. It is for this reason that these calls are protected, not public. Access to PtGates by user code is accomplished by means of another class, CriticalSection. The CriticalSection class is a friend of class PtGate.



Top Up Prev Next Bottom Contents Index Search

Copyright © 1990-1997, University of California. All rights reserved.