class GThread

Thread class.

Public Methods

[more] GThread(int stacksize = -1)
Constructs a new thread object.
[more] ~GThread()
Destructor.
[more]int create(void (*entry)(void*), void *arg)
Starts the thread.
[more]void terminate()
Terminates a thread with extreme prejudice.
[more]static int yield()
Causes the current thread to relinquish the processor.
[more]static void* current()
Returns a value which uniquely identifies the current thread.
[more]friendclass GMonitorclass cotaskclass cotaskstatic *task int select(int nfds, fd_set*, fd_set*, fd_set*, struct timeval*)
Replaces system call select (COTHREADS only).
[more]static void get_select(int &nfds, fd_set*, fd_set*, fd_set*, unsigned long &timeout)
Provide arguments for system call select (COTHREADS only).
[more]static void set_scheduling_callback(void (*call)(int))
Install hooks in the scheduler (COTHREADS only).


Documentation

Thread class. A multithreaded process is composed of a main execution thread and of several secondary threads. Each secondary thread is represented by a GThread object. The amount of memory required for the stack of a secondary thread is defined when the GThread object is constructed. The execution thread is started when function create is called. The destructor of class GThread waits until the thread terminanes. Note that the execution can be terminated at any time (with possible prejudice) by calling terminate.

Several static member functions control the thread scheduler. Function yield relinquishes the processor to another thread. Function select (COTHREADS only) provides a thread-aware replacement for the well-known unix system call select.

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.

o GThread(int stacksize = -1)
Constructs a new thread object. Memory is allocated for the thread, but the thread is not started. Argument stacksize is used by the COTHREADS model only for specifying the amount of memory needed for the processor stack. A negative value will be replaced by a suitable default value of 128Kb. A minimum value of 32Kb is silently enforced.

o ~GThread()
Destructor. Destroying the thread object while the thread is running is perfectly ok since it only destroys the thread identifier. Execution will continue without interference.

oint create(void (*entry)(void*), void *arg)
Starts the thread. The new thread executes function argument arg. The thread terminates when the function returns. A thread cannot be restarted after its termination. You must create a new GThread object.

ovoid terminate()
Terminates a thread with extreme prejudice. The thread is removed from the scheduling list. Execution terminates regardless of the execution status of the thread function. Automatic variables may or may not be destroyed. This function must be considered as a last resort since memory may be lost.

ostatic int yield()
Causes the current thread to relinquish the processor. The scheduler selects a thread ready to run and transfers control to that thread. The actual effect of yield heavily depends on the selected implementation. Function yield usually returns zero when the execution of the current thread is resumed. It may return a positive number when it can determine that the current thread will remain the only runnable thread for some time. You may then call function get_select to obtain more information.

ostatic void* current()
Returns a value which uniquely identifies the current thread.

ofriendclass GMonitorclass cotaskclass cotaskstatic *task int select(int nfds, fd_set*, fd_set*, fd_set*, struct timeval*)
Replaces system call select (COTHREADS only). The COTHREADS model does not redefine system function. System functions therefore can potentially block the whole process (instead of blocking the current thread only) because the system is not aware of the COTHREADS scheduler. The function GThread::select is a COTHREADS-aware replacement for the well known system function select. You can also use GThread::select for making sure that calls to system functions will not block the entire process, as demonstrated below:
      int 
      gthread_read(int fd, void *buffer, size_t len) 
      {
        fd_set rdset; 
        FD_ZERO(&rdset); 
        FD_SET(fd, &rdset);
        GThread::select(fd+1, &rdset, 0, 0, 0);
        return read(fd, buffer, len);
      }
      

ostatic void get_select(int &nfds, fd_set*, fd_set*, fd_set*, unsigned long &timeout)
Provide arguments for system call select (COTHREADS only). It may be appropriate to call the real system call select if the current thread is the only thread ready to run. Other threads however may wake up when certain file descriptors are ready or when a certain delay expires. Function get_select returns this information by filling the three usual file descriptor sets (similar to the arguments of system call select). It also returns a timeout timeout expressed in milliseconds. Note that this timeout is zero is the current thread is not the sole thread ready to run.

ostatic void set_scheduling_callback(void (*call)(int))
Install hooks in the scheduler (COTHREADS only). The hook function call is called when a new thread is created (argument is GThread::CallbackCreate), when a thread terminates (argument is GThread::CallbackTerminate), or when thread is unblocked (argument is GThread::CallbackUnblock). This callback can be useful in certain GUI toolkits where the most convenient method for scheduling the threads consists in setting a timer event that calls yield.


This class has no child classes.

Alphabetic index HTML hierarchy of classes or Java


DjVu is a trademark of LizardTech, Inc.
All other products mentioned are registered trademarks or trademarks of their respective companies.