This class defines a thread of execution in the system.
Run Time Type functions
Comparison functions
I/O functions
Miscellaneous functions
This class defines a thread of execution in the system. Athread is an independent flow of processor instructions. This differs from aprocess which also embodies a program address space and resource allocation. So threads can share memory and resources as they run in the context of a given process. A process always contains at least one thread. This is reflected in this library by the PProcess class being descended from the PThread class.The implementation of a thread is platform dependent. Not all platforms support concurrent threads within a process or even concurrent processes! For example, MS-DOS has no form of multi-threading or multi-processing, Microsoft Windows has a cooperative multi-processing but no multi-threading. Unix has full pre-emptive multi-processing but most cannot do multiple threads within that process while some Unix systems and Windows NT have full preemptive proceses and threads.
If a platform does not directly support multiple threads, the library will them using a cooperative co-routine technique. This requires that each thread of execution within a process, voluntarily yields control to other threads. This will occur if the thread is blocked inside an I/O function on a PChannel or when the PThread::Yield() function is explicitly called.
Note that this is cooperative. An endless loop will stop all threads in a process, possibly all processes on some platforms. If a lengthy operation is to take place that does not involve blocking I/O, eg pure computation or disk file I/O, then it is the responsiblity of the programmer to assure enough yielding for background threads to execute.
Terminated LowPriority
Terminated NormalPriority
Terminated HighPriority
Terminated HighestPriority
NoAutoDeleteThread
Note that the exact timing of the execution of code in threads can never be predicted. Thus you you can get a race condition on intialising a descendent class. To avoid this problem a thread is always started suspended. You must call the Resume() function after your descendent class construction is complete.
If synchronisation is required between threads then the use of semaphores is essential.
If the deletion is set to AutoDeleteThread then the PThread is assumed to be allocated with the new operator and may be freed using the delete operator as soon as the thread is terminated or executes to completion (usually the latter).
The stack size specified is not simply in bytes. It is a value that is multiplied by a factor into bytes depending on the target platform. For example a Unix system with a RISC processor may use significantly more stack than an MS-DOS platform. These sizes are normalised to the "stack factor" provided here. For some platforms, eg Windows NT, the stack size is only an initial size and the stack will automatically be increased as required.
Note that the correct way for a thread to terminate is to return from
the Main() function.
~PThread()
If the thread is still running then this function is ignored.
Note that the correct way for a thread to terminate is to return from
the Main() function or self terminate by calling
Terminate() within the context of the thread which can then
assure that all resources are cleaned up.
If susp is TRUE this increments an internal count of
suspensions that must be matched by an equal number of calls to
Resume() or Suspend(FALSE) before the
thread actually executes again. If susp is FALSE then this decrements the internal count of
suspensions. If the count is <= 0 then the thread will run. Note that
the thread will not be suspended until an equal number of
Suspend(TRUE) calls are made.
virtual void Terminate()
virtual BOOL IsTerminated() const
void WaitForTermination() const
virtual void Suspend( BOOL susp = TRUE )
virtual void Resume()
virtual BOOL IsSuspended() const
static void Sleep( const PTimeInterval & delay Time interval to sleep for. )
virtual void SetPriority( Priority priorityLevel )
virtual Priority GetPriority() const
virtual void SetAutoDelete( AutoDeleteFlag deletion = AutoDeleteThread )
void SetNoAutoDelete()
virtual PString GetThreadName() const
virtual void SetThreadName( const PString & name )
Note that the correct way for a thread to terminate is to return from this function.
If the platform directly supports multiple threads then this function
will do nothing.
static PThread* Current()
static void Yield()
static PThread* Create( const PNotifier & notifier, INT parameter = 0, AutoDeleteFlag deletion = AutoDeleteThread, Priority priorityLevel = NormalPriority, const PString & threadName = "", PINDEX stackSize = 10000 )
parameter - Parameter value to pass to notifier.
deletion - Automatically delete PThread instance on termination of thread.
priorityLevel - Initial priority of thread.
threadName - The name of the thread (for Debug/Trace)
stackSize - Stack size on some platforms
Alphabetic index HTML hierarchy of classes or Java