#include <thread.h>
Inheritance diagram for PThread:
Construction | |
enum | Priority { LowestPriority, LowPriority, NormalPriority, HighPriority, HighestPriority, NumPriorities } |
Codes for thread priorities. More... | |
enum | AutoDeleteFlag { AutoDeleteThread, NoAutoDeleteThread } |
Codes for thread autodelete flag. More... | |
PThread (PINDEX, AutoDeleteFlag deletion=AutoDeleteThread, Priority priorityLevel=NormalPriority, const PString &threadName=PString::Empty()) | |
~PThread () | |
Control functions | |
virtual void | Restart () |
virtual void | Terminate () |
virtual PBoolean | IsTerminated () const |
void | WaitForTermination () const |
PBoolean | WaitForTermination (const PTimeInterval &maxWait) const |
virtual void | Suspend (PBoolean susp=PTrue) |
virtual void | Resume () |
virtual PBoolean | IsSuspended () const |
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) |
static void | Sleep (const PTimeInterval &delay) |
Suspend the current thread for the specified amount of time. | |
Miscellaneous | |
virtual PThreadIdentifier | GetThreadId () const |
virtual void | Main ()=0 |
static PThreadIdentifier | GetCurrentThreadId () |
static PThread * | Current () |
static void | Yield () |
static PThread * | Create (const PNotifier ¬ifier, INT parameter=0, AutoDeleteFlag deletion=AutoDeleteThread, Priority priorityLevel=NormalPriority, const PString &threadName=PString::Empty(), PINDEX stackSize=65536) |
static PThread * | Create (const PNotifier ¬ifier, const PString &threadName) |
Public Member Functions | |
int | PXBlockOnChildTerminate (int pid, const PTimeInterval &timeout) |
int | PXBlockOnIO (int handle, int type, const PTimeInterval &timeout) |
void | PXAbortBlock () const |
Overrides from #PObject | |
void | PrintOn (ostream &strm) const |
Protected Member Functions | |
void | InitialiseProcessThread () |
Friends | |
class | PProcess |
The implementation of a thread is platform dependent, but it is assumed that the platform has some support for native threads. Previous versions of PTLib/PWLib have some support for co-operative threads, but this has been removed.
enum PThread::Priority |
Codes for thread priorities.
PThread::PThread | ( | PINDEX | , | |
AutoDeleteFlag | deletion = AutoDeleteThread , |
|||
Priority | priorityLevel = NormalPriority , |
|||
const PString & | threadName = PString::Empty() | |||
) |
Create a new thread instance. Unless the startSuspended parameter is PTrue, the threads Main() function is called to execute the code for the thread.
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 argument retained only for source code compatibility for previous implementations. It is not used in the current code and may be removed in subsequent versions.
deletion | Automatically delete PThread instance on termination of thread. |
priorityLevel | Initial priority of thread. |
threadName | The name of the thread (for Debug/Trace) |
PThread::~PThread | ( | ) |
Destroy the thread, this simply calls the Terminate() function with all its restrictions and penalties. See that function for more information.
Note that the correct way for a thread to terminate is to return from the Main() function.
void PThread::PrintOn | ( | ostream & | strm | ) | const [virtual] |
virtual void PThread::Restart | ( | ) | [virtual] |
Restart a terminated thread using the same stack priority etc that was current when the thread terminated.
If the thread is still running then this function is ignored.
virtual void PThread::Terminate | ( | ) | [virtual] |
Terminate the thread. It is highly recommended that this is not used except in abnormal abort situations as not all clean up of resources allocated to the thread will be executed. This is especially true in C++ as the destructors of objects that are automatic variables are not called causing at the very least the possiblity of memory leaks.
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.
Reimplemented in PProcess, and PServiceProcess.
virtual PBoolean PThread::IsTerminated | ( | ) | const [virtual] |
Determine if the thread has been terminated or ran to completion.
void PThread::WaitForTermination | ( | ) | const |
Block and wait for the thread to terminate.
PBoolean PThread::WaitForTermination | ( | const PTimeInterval & | maxWait | ) | const |
virtual void PThread::Suspend | ( | PBoolean | susp = PTrue |
) | [virtual] |
Suspend or resume the thread.
If susp is PTrue this increments an internal count of suspensions that must be matched by an equal number of calls to Resume()# or Suspend(PFalse) before the thread actually executes again.
If susp is PFalse 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(PTrue) calls are made.
susp | Flag to suspend or resume a thread. |
virtual void PThread::Resume | ( | ) | [virtual] |
Resume thread execution, this is identical to Suspend(PFalse).
The Resume() method may be called from within the constructor of a PThread descendant. However, the Resume() should be in the constructor of the most descendant class. So, if you have a class B (which is descended of PThread), and a class C (which is descended of B), placing the call to Resume() in the constructor of B is unwise.
If you do place a call to Resume() in the constructor, it should be at the end of the constructor, after all the other initialisation in the constructor.
The reason the call to Resume() should be at the end of the construction process is simple - you want the thread to start when all the variables in the class have been correctly initialised.
virtual PBoolean PThread::IsSuspended | ( | ) | const [virtual] |
static void PThread::Sleep | ( | const PTimeInterval & | delay | ) | [static] |
Suspend the current thread for the specified amount of time.
delay | Time interval to sleep for. |
virtual void PThread::SetPriority | ( | Priority | priorityLevel | ) | [virtual] |
Set the priority of the thread relative to other threads in the current process.
priorityLevel | New priority for thread. |
virtual Priority PThread::GetPriority | ( | ) | const [virtual] |
Get the current priority of the thread in the current process.
virtual void PThread::SetAutoDelete | ( | AutoDeleteFlag | deletion = AutoDeleteThread |
) | [virtual] |
Set the flag indicating thread object is to be automatically deleted when the thread ends.
deletion | New auto delete setting. |
void PThread::SetNoAutoDelete | ( | ) | [inline] |
Reet the flag indicating thread object is to be automatically deleted when the thread ends.
virtual PString PThread::GetThreadName | ( | ) | const [virtual] |
Get the name of the thread. Thread names are a optional debugging aid.
Reimplemented in PProcess.
virtual void PThread::SetThreadName | ( | const PString & | name | ) | [virtual] |
Change the name of the thread. Thread names are a optional debugging aid.
name | New name for the thread. |
Reimplemented in PProcess.
virtual PThreadIdentifier PThread::GetThreadId | ( | ) | const [virtual] |
Get operating system specific thread identifier for this thread. Note that the return value from these functions is only valid if called by the owning thread. Calling this function for another thread that may be terminating is a very bad idea.
static PThreadIdentifier PThread::GetCurrentThreadId | ( | ) | [static] |
virtual void PThread::Main | ( | ) | [pure virtual] |
User override function for the main execution routine of the thread. A descendent class must provide the code that will be executed in the thread within this function.
Note that the correct way for a thread to terminate is to return from this function.
Implemented in PThreadMain, PThread1Arg< Arg1Type >, PThread2Arg< Arg1Type, Arg2Type >, PThread3Arg< Arg1Type, Arg2Type, Arg3Type >, PThreadObj< ObjType >, PThreadObj1Arg< ObjType, Arg1Type >, PThreadObj2Arg< ObjType, Arg1Type, Arg2Type >, and PHTTPServiceThread.
static PThread* PThread::Current | ( | ) | [static] |
Get the currently running thread object instance. It is possible, even likely, that the smae code may be executed in the context of differenct threads. Under some circumstances it may be necessary to know what the current codes thread is and this static function provides that information.
Reimplemented in PProcess, PServiceProcess, and PHTTPServiceProcess.
static void PThread::Yield | ( | ) | [static] |
Yield to another thread without blocking. This duplicates the implicit thread yield that may occur on some I/O operations or system calls.
This may not be implemented on all platforms.
static PThread* PThread::Create | ( | const PNotifier & | notifier, | |
INT | parameter = 0 , |
|||
AutoDeleteFlag | deletion = AutoDeleteThread , |
|||
Priority | priorityLevel = NormalPriority , |
|||
const PString & | threadName = PString::Empty() , |
|||
PINDEX | stackSize = 65536 | |||
) | [static] |
Create a simple thread executing the specified notifier. This creates a simple PThread class that automatically executes the function defined by the PNotifier in the context of a new thread.
notifier | Function to execute in thread. |
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 |
static PThread* PThread::Create | ( | const PNotifier & | notifier, | |
const PString & | threadName | |||
) | [inline, static] |
notifier | Function to execute in thread. |
threadName | The name of the thread (for Debug/Trace) |
void PThread::InitialiseProcessThread | ( | ) | [protected] |
int PThread::PXBlockOnChildTerminate | ( | int | pid, | |
const PTimeInterval & | timeout | |||
) |
int PThread::PXBlockOnIO | ( | int | handle, | |
int | type, | |||
const PTimeInterval & | timeout | |||
) |
void PThread::PXAbortBlock | ( | ) | const |
friend class PProcess [friend] |