kviewshell
GMonitor Class Reference
Monitor class. More...
#include <GThreads.h>
Public Member Functions | |
GThreads.h | |
Files #"GThreads.h"# and #"GThreads.cpp"# implement common entry points for multithreading on multiple platforms. Each execution thread is represented by an instance of class {GThread}. Synchronization is provided by class {GMonitor} which implements a monitor (C.A.R Hoare, Communications of the ACM, 17(10), 1974). The value of compiler symbol THREADMODEL# selects an appropriate implementation for these classes. The current implementation supports the following values: {description} [-DTHREADMODEL=NOTHREADS] Dummy implementation. This is a good choice when the multithreading features are not required, because it minimizes the portability problems. This is currently the default when compiling under Unix. [-DTHREADMODEL=WINTHREADS] Windows implementation. This is the default when compiling under Windows. [-DTHREADMODEL=MACTHREADS] Macintosh implementation, which is based on the MacOS cooperative model. The current implementation does not yet fully support synchronization. This is the default when compiling under MacOS. [-DTHREADMODEL=POSIXTHREADS] Posix implementation. This implementation also supports DCE threads. The behavior of the code is subject to the quality of the system implementation of Posix threads. [-DTHREADMODEL=COTHREADS] Custom cooperative threads. These custom threads do not redefine system calls. Before executing a potentially blocking system function, each thread must explicitly check whether it is going to block and yield control explicitly if this is the case. This code must be compiled with a patched version of egcs-1.1.1 {http://egcs.cygnus.com}. The patch addresses exception thread-safety and is provided in #"@Tools/libgcc2.c.diff"#. Once you get the right compiler, this implementation is remarkably compact and portable. A variety of processors are supported, including mips, intel, sparc, hppa, and alpha. [-DTHREADMODEL=JRITHREADS] Java implementation hooks. Multi-threading within a Netscape plugin can be tricky. A simple idea however consists of implementing the threading primitives in Java and to access them using JRI. The classes just contain a JRIGlobalRef. This is not a real implementation since everything (Java code, native functions, stubs, exception thread safety) must be addressed by the plugin source code. Performance may be a serious issue. {description} { Portability}: The simultaneous use of threads and exceptions caused a lot of portability headaches under Unix. We eventually decided to implement the COTHREADS cooperative threads (because preemptive threads have more problems) and to patch EGCS in order to make exception handling COTHREAD-safe. Portable threads From: Leon Bottou, 1/31/2002 Almost unchanged by Lizardtech. GSafeFlags should go because it not as safe as it claims.
| |
void | broadcast () |
void | enter () |
GMonitor () | |
void | leave () |
void | signal () |
void | wait (unsigned long timeout) |
void | wait () |
~GMonitor () |
Detailed Description
Monitor class.Monitors have been first described in (C.A.R Hoare, Communications of the ACM, 17(10), 1974). This mechanism provides the basic mutual exclusion (mutex) and thread notification facilities (condition variables).
Only one thread can own the monitor at a given time. Functions {enter} and {leave} can be used to acquire and release the monitor. This mutual exclusion provides an efficient way to protect segment of codes ({critical sections}) which should not be simultaneously executed by two threads. Class {GMonitorLock} provides a convenient way to do this effectively.
When the thread owning the monitor calls function {wait}, the monitor is released and the thread starts waiting until another thread calls function {signal} or {broadcast}. When the thread wakes-up, it re-acquires the monitor and function wait# returns. Since the signaling thread must acquire the monitor before calling functions signal# and broadcast#, the signaled thread will not be able to re-acquire the monitor until the signaling thread(s) releases the monitor.
{ Note} --- Both the copy constructor and the copy operator are declared as private members. It is therefore not possible to make multiple copies of instances of this class, as implied by the class semantic.
Definition at line 362 of file GThreads.h.
Constructor & Destructor Documentation
GMonitor::GMonitor | ( | ) | [inline] |
Definition at line 445 of file GThreads.h.
GMonitor::~GMonitor | ( | ) | [inline] |
Definition at line 446 of file GThreads.h.
Member Function Documentation
void GMonitor::broadcast | ( | ) | [inline] |
Signals all waiting threads.
Function broadcast# wakes up all the waiting threads for this monitor. An exception is thrown if this function is called by a thread which does not own the monitor.
Definition at line 452 of file GThreads.h.
void GMonitor::enter | ( | ) | [inline] |
Enters the monitor.
If the monitor is acquired by another thread this function waits until the monitor is released. The current thread then acquires the monitor. Calls to enter# and leave# may be nested.
Definition at line 447 of file GThreads.h.
void GMonitor::leave | ( | ) | [inline] |
Leaves the monitor.
The monitor counts how many times the current thread has entered the monitor. Function leave# decrement this count. The monitor is released when this count reaches zero. An exception is thrown if this function is called by a thread which does not own the monitor.
Definition at line 448 of file GThreads.h.
void GMonitor::signal | ( | ) | [inline] |
Signals one waiting thread.
Function signal# wakes up at most one of the waiting threads for this monitor. An exception is thrown if this function is called by a thread which does not own the monitor.
Definition at line 451 of file GThreads.h.
void GMonitor::wait | ( | unsigned long | timeout | ) | [inline] |
Waits until the monitor is signaled or a timeout is reached.
The current thread atomically releases the monitor and waits until another thread calls function signal# or broadcast# or a maximum of timeout# milliseconds. Function wait# then re-acquires the monitor and returns. An exception is thrown if this function is called by a thread which does not own the monitor.
Definition at line 450 of file GThreads.h.
void GMonitor::wait | ( | ) | [inline] |
Waits until the monitor is signaled.
The current thread atomically releases the monitor and waits until another thread calls function signal# or broadcast#. Function wait# then re-acquires the monitor and returns. An exception is thrown if this function is called by a thread which does not own the monitor.
Reimplemented in GEvent.
Definition at line 449 of file GThreads.h.
The documentation for this class was generated from the following file: