30 #ifndef PTLIB_THREAD_H
31 #define PTLIB_THREAD_H
116 Priority priorityLevel = NormalPriority,
259 Priority priorityLevel
320 PThreadIdentifier GetThreadId()
const
326 static PThreadIdentifier GetCurrentThreadId()
335 PUniqueThreadIdentifier GetUniqueIdentifier()
const
341 static PUniqueThreadIdentifier GetCurrentUniqueIdentifier()
347 static PINDEX GetTotalCount()
357 friend ostream &
operator<<(ostream & strm,
const Times & times)
360 float AsPercentage()
const
392 PThreadIdentifier
id,
400 std::vector<Times> & times
403 std::list<Times> & times
406 std::set<Times> & times
423 Times & previousTimes,
425 PThreadIdentifier
id = PNullThreadIdentifier
439 virtual void Main() = 0;
469 Priority priorityLevel = NormalPriority,
490 virtual void *
Allocate()
const = 0;
491 virtual void Deallocate(
void * ptr)
const = 0;
494 typedef std::map<PUniqueThreadIdentifier, void *> DataMap;
495 mutable DataMap m_data;
504 void InternalThreadMain();
505 void InternalPreMain();
506 void InternalPostMain();
507 void InternalDestroy();
541 #include "msos/ptlib/thread.h"
543 #include "unix/ptlib/thread.h"
603 template<
typename Functor>
610 bool autoDel =
false,
611 const char * name = NULL,
612 PThread::Priority priority = PThread::NormalPriority
647 template<
typename Arg1Type>
657 bool autoDel =
false,
658 const char * name = NULL,
659 PThread::Priority priority = PThread::NormalPriority
695 template<
typename Arg1Type,
typename Arg2Type>
700 typedef void (*
FnType)(Arg1Type arg1, Arg2Type arg2);
705 bool autoDel =
false,
706 const char * name = NULL,
707 PThread::Priority priority = PThread::NormalPriority
744 template<
typename Arg1Type,
typename Arg2Type,
typename Arg3Type>
749 typedef void (*
FnType)(Arg1Type arg1, Arg2Type arg2, Arg3Type arg3);
755 bool autoDel =
false,
756 const char * name = NULL,
757 PThread::Priority priority = PThread::NormalPriority
800 template <
typename ObjType>
810 bool autoDel =
false,
811 const char * name = NULL,
812 PThread::Priority priority = PThread::NormalPriority
818 , m_function(function)
855 template <
class ObjType,
typename Arg1Type>
866 bool autoDel =
false,
867 const char * name = NULL,
868 PThread::Priority priority = PThread::NormalPriority
874 , m_function(function)
896 template <
class ObjType,
typename Arg1Type,
typename Arg2Type>
908 bool autoDel =
false,
909 const char * name = NULL,
910 PThread::Priority priority = PThread::NormalPriority
916 , m_function(function)
946 template <
class Storage_T>
959 virtual void *
Allocate()
const {
return new Storage_T(); }
964 #define P_HAS_THREADLOCAL_STORAGE 1 // For backward compatbility
967 #endif // PTLIB_THREAD_H
Arg2Type m_arg2
Definition: thread.h:937
virtual void Deallocate(void *ptr) const
Definition: thread.h:960
~PThreadObj2Arg()
Definition: thread.h:923
void WaitForTermination() const
Block and wait for the thread to terminate.
Definition: channel.h:737
AutoDeleteFlag
Codes for thread autodelete flag.
Definition: thread.h:83
This class defines a thread synchronisation object.
Definition: semaphor.h:74
virtual PString GetThreadName() const
Get the name of the thread.
~PThreadObj()
Definition: thread.h:823
Arg1Type m_arg1
Definition: thread.h:728
Times for execution of the thread.
Definition: thread.h:351
FnType m_function
Definition: thread.h:778
This class defines an arbitrary time interval to millisecond accuracy.
Definition: timeint.h:51
virtual PBoolean IsTerminated() const
Determine if the thread has been terminated or ran to completion.
virtual void Main()
User override function for the main execution routine of the thread.
Definition: thread.h:581
Class specialisation for PNotifierTemplate<P_INT_PTR>
Storage_T * Ptr_T
Definition: thread.h:950
Ptr_T Get() const
Definition: thread.h:953
void(* FnType)()
Definition: thread.h:568
static PThread * Create(const PNotifier ¬ifier, const PString &threadName)
Definition: thread.h:473
This class defines an absolute time and date.
Definition: ptime.h:49
Arg1Type m_arg1
Definition: thread.h:936
~PThread()
Destroy the thread, this simply calls the Terminate() function with all its restrictions and penaltie...
This class implements critical section mutexes using the most efficient mechanism available on the ho...
Definition: mutex.h:270
static PString GetCurrentThreadName()
Get the current threads name.
Definition: thread.h:297
Arg2Type m_arg2
Definition: thread.h:780
PThread1Arg(Arg1Type arg1, FnType function, bool autoDel=false, const char *name=NULL, PThread::Priority priority=PThread::NormalPriority)
Definition: thread.h:654
virtual void Main()=0
User override function for the main execution routine of the thread.
Arg1Type m_arg1
Definition: thread.h:679
Type
Definition: thread.h:529
virtual void Resume()
Resume thread execution, this is identical to Suspend(false).
PUniqueThreadIdentifier m_uniqueId
Unique thread identifier.
Definition: thread.h:374
PThread2Arg(Arg1Type arg1, Arg2Type arg2, FnType function, bool autoDel=false, const char *name=NULL, PThread::Priority priority=PThread::NormalPriority)
Definition: thread.h:701
FnType m_function
Definition: thread.h:727
~PThreadFunctor()
Definition: thread.h:620
PCriticalSection m_threadNameMutex
Definition: thread.h:534
PThreadMain(FnType function, bool autoDel=false)
Definition: thread.h:569
P_ALIGN_FIELD(ObjTypeFn, m_function, 16)
Arg3Type m_arg3
Definition: thread.h:781
static bool WaitAndDelete(PThread *&thread, const PTimeInterval &maxWait=10000, PMutex *mutex=NULL, bool lock=true)
Wait for thread termination and delete object.
Ptr_T operator->() const
Definition: thread.h:955
~PThread3Arg()
Definition: thread.h:767
virtual void Suspend(PBoolean susp=true)
Suspend or resume the thread.
Define some templates to simplify the declaration of simple PThread descendants with one or two param...
Definition: thread.h:564
P_ALIGN_FIELD(ObjTypeFn, m_function, 16)
Arg1Type m_arg1
Definition: thread.h:893
void PrintOn(ostream &strm) const
Standard stream print function.
static void Sleep(const PTimeInterval &delay)
Suspend the current thread for the specified amount of time.
static PThread * Create(const PNotifier ¬ifier, INT parameter=0, AutoDeleteFlag deletion=AutoDeleteThread, Priority priorityLevel=NormalPriority, const PString &threadName=PString::Empty(), PINDEX stackSize=0)
Create a simple thread executing the specified notifier.
virtual void Main()
User override function for the main execution routine of the thread.
Definition: thread.h:672
virtual void Terminate()
Terminate the thread.
void(* FnType)(Arg1Type arg1)
Definition: thread.h:652
P_DECLARE_TRACED_ENUM(Priority, LowestPriority, LowPriority, NormalPriority, HighPriority, HighestPriority)
Codes for thread priorities.
This class represents an operating system process.
Definition: pprocess.h:106
PString m_name
Name of thread.
Definition: thread.h:372
PString m_threadName
Definition: thread.h:533
~PThreadObj1Arg()
Definition: thread.h:880
~PThread1Arg()
Definition: thread.h:667
friend class PThreadLocalStorageData
Definition: thread.h:497
static PString GetIdentifiersAsString(PThreadIdentifier tid, PUniqueThreadIdentifier uid)
Convert to thread identifers as a string.
void(* FnType)(Arg1Type arg1, Arg2Type arg2)
Definition: thread.h:700
ObjType & m_object
Definition: thread.h:834
ObjType & m_object
Definition: thread.h:934
virtual void SetAutoDelete(AutoDeleteFlag deletion=AutoDeleteThread)
Set the flag indicating thread object is to be automatically deleted when the thread ends...
void(* FnType)(Arg1Type arg1, Arg2Type arg2, Arg3Type arg3)
Definition: thread.h:749
PThread3Arg(Arg1Type arg1, Arg2Type arg2, Arg3Type arg3, FnType function, bool autoDel=false, const char *name=NULL, PThread::Priority priority=PThread::NormalPriority)
Definition: thread.h:750
bool PBoolean
Definition: object.h:174
~PThreadMain()
Definition: thread.h:576
PThreadObj(ObjType &obj, ObjTypeFn function, bool autoDel=false, const char *name=NULL, PThread::Priority priority=PThread::NormalPriority)
Definition: thread.h:807
static void GetTimes(std::list< Times > ×)
PThreadIdentifier m_threadId
Operating system thread ID.
Definition: thread.h:373
virtual void Deallocate(void *ptr) const =0
The character string class.
Definition: pstring.h:108
static void Yield()
Yield to another thread without blocking.
FnType m_function
Definition: thread.h:587
PTimeInterval m_kernel
Total kernel CPU time in milliseconds.
Definition: thread.h:376
Don't delete thread as it may not be on heap.
Definition: thread.h:88
virtual void Main()
User override function for the main execution routine of the thread.
Definition: thread.h:772
Functor & m_funct
Definition: thread.h:631
void SetNoAutoDelete()
Reet the flag indicating thread object is to be automatically deleted when the thread ends...
Definition: thread.h:279
This class defines a thread mutual exclusion object.
Definition: mutex.h:101
This class defines a thread of execution in the system.
Definition: thread.h:66
PTimeInterval m_real
Total real time since thread start in milliseconds.
Definition: thread.h:375
P_ALIGN_FIELD(ObjTypeFn, m_function, 16)
virtual void Main()
User override function for the main execution routine of the thread.
Definition: thread.h:721
static int GetPercentageCPU(Times &previousTimes, const PTimeInterval &period=PTimeInterval(0, 1), PThreadIdentifier id=PNullThreadIdentifier)
Calculate the percentage CPU used over a period of time.
PUniqueThreadIdentifier m_uniqueId
Definition: thread.h:537
static const PString & Empty()
Return an empty string.
void(ObjType::* ObjTypeFn)(Arg1Type)
Definition: thread.h:860
Arg2Type m_arg2
Definition: thread.h:729
PTime m_sample
Time of sample.
Definition: thread.h:378
~PThreadLocalStorage()
Definition: thread.h:952
PThread(PINDEX stack, AutoDeleteFlag deletion=AutoDeleteThread, Priority priorityLevel=NormalPriority, const PString &threadName=PString::Empty())
Create a new thread instance.
virtual void * Allocate() const
Definition: thread.h:959
static unsigned GetNumProcessors()
Get number of processors, or processor cores, this machine has available.
void Main()
User override function for the main execution routine of the thread.
Definition: thread.h:828
enum PThread::Type m_type
void Main()
User override function for the main execution routine of the thread.
Definition: thread.h:885
virtual void * Allocate() const =0
bool IsAutoDelete() const
Definition: thread.h:479
static PThread * Current()
Get the currently running thread object instance.
virtual void Restart()
Restart a terminated thread using the same stack priority etc that was current when the thread termin...
void(ObjType::* ObjTypeFn)(Arg1Type, Arg2Type)
Definition: thread.h:901
Automatically delete thread object on termination.
Definition: thread.h:85
void(ObjType::* ObjTypeFn)()
Definition: thread.h:805
virtual PBoolean IsSuspended() const
Determine if the thread is currently suspended.
PINDEX m_originalStackSize
Definition: thread.h:531
virtual void SetThreadName(const PString &name)
Change the name of the thread.
PThreadObj1Arg(ObjType &obj, Arg1Type arg1, ObjTypeFn function, bool autoDel=false, const char *name=NULL, PThread::Priority priority=PThread::NormalPriority)
Definition: thread.h:862
virtual Priority GetPriority() const
Get the current priority of the thread in the current process.
Definition: object.h:1537
Storage_T & operator*() const
Definition: thread.h:956
void Main()
User override function for the main execution routine of the thread.
Definition: thread.h:928
~PThread2Arg()
Definition: thread.h:716
virtual ~LocalStorageBase()
Definition: thread.h:485
PThreadFunctor(Functor &funct, bool autoDel=false, const char *name=NULL, PThread::Priority priority=PThread::NormalPriority)
Definition: thread.h:608
virtual void Main()
User override function for the main execution routine of the thread.
Definition: thread.h:625
ObjType & m_object
Definition: thread.h:891
Ultimate parent class for all objects in the class library.
Definition: object.h:2204
void ThreadDestroyed(PThread &thread)
This class defines a thread synchronisation object.
Definition: syncpoint.h:63
Arg1Type m_arg1
Definition: thread.h:779
friend class PExternalThread
Definition: thread.h:510
PPROFILE_EXCLUDE(PThreadIdentifier GetThreadId() const )
PThreadObj2Arg(ObjType &obj, Arg1Type arg1, Arg2Type arg2, ObjTypeFn function, bool autoDel=false, const char *name=NULL, PThread::Priority priority=PThread::NormalPriority)
Definition: thread.h:903
PTimeInterval m_user
Total user CPU time in milliseconds.
Definition: thread.h:377
PThreadIdentifier m_threadId
Definition: thread.h:536
virtual void SetPriority(Priority priorityLevel)
Set the priority of the thread relative to other threads in the current process.
Thread local storage base class, see PThreadLocalStorage for template.
Definition: thread.h:482
virtual void * GetStorage() const
FnType m_function
Definition: thread.h:678