library: libThread #include "TThread.h"

# TThread

class description - source file - inheritance tree (.pdf)

## class TThread : public TNamed

| Inheritance Chart:
`TObject`
| <- | `TNamed`
| <- | `TThread` |

        private:

           static void AfterCancel(TThread* th)
                  void Constructor()
          virtual void DoError(Int_t level, const char* location, const char* fmt, va_list va) const
                  void ErrorHandler(int level, const char* location, const char* fmt, va_list ap) const
          static void* Function(void* ptr)
           static void Init()
                  void SetComment(const char* txt = "0")
          static Int_t XARequest(const char* xact, Int_t nb, void** ar, Int_t* iret)

        public:

                             TThread(TThread::VoidRtnFunc_t fn, void* arg = 0, TThread::EPriority pri = kNormalPriority)
                             TThread(const char* thname, TThread::VoidRtnFunc_t fn, void* arg = 0, TThread::EPriority pri = kNormalPriority)
                             TThread(Int_t id = 0)
                             TThread(const TThread&)
                     virtual ~TThread()
                static Int_t CancelPoint()
              static TClass* Class()
                static Int_t CleanUp()
                static Int_t CleanUpPop(Int_t exe = 0)
                static Int_t CleanUpPush(void* free, void* arg = 0)
                virtual void Delete(Option_t* option = "")
                static Int_t Delete(TThread* th)
                static Int_t Exists()
                static Int_t Exit(void* ret = 0)
                      Long_t GetId() const
          TThread::EPriority GetPriority() const
             TThread::EState GetState() const
             static TThread* GetThread(Long_t id)
             static TThread* GetThread(const char* name)
                static Int_t GetTime(ULong_t* absSec, ULong_t* absNanoSec)
             virtual TClass* IsA() const
                      Long_t Join(void** ret = 0)
               static Long_t Join(Long_t id, void** ret = 0)
                       Int_t Kill()
                static Int_t Kill(Long_t id)
                static Int_t Kill(const char* name)
                static Int_t Lock()
                    TThread& operator=(const TThread&)
                 static void Printf(const char* fmt)
                 static void Ps()
                 static void ps()
                       Int_t Run(void* arg = 0)
             static TThread* Self()
               static Long_t SelfId()
                static Int_t SetCancelAsynchronous()
                static Int_t SetCancelDeferred()
                static Int_t SetCancelOff()
                static Int_t SetCancelOn()
                        void SetPriority(TThread::EPriority pri)
                virtual void ShowMembers(TMemberInspector& insp, char* parent)
                static Int_t Sleep(ULong_t secs, ULong_t nanos = 0)
                virtual void Streamer(TBuffer& b)
                        void StreamerNVirtual(TBuffer& b)
                static Int_t TryLock()
               static void** Tsd(void* dflt, Int_t k)
                static Int_t UnLock()
                 static void XAction()

### Data Members

        private:

                    TThread* fNext           pointer to next thread
                    TThread* fPrev           pointer to prev thread
                   TThread** fHolder         pointer to holder of this (delete only)
          TThread::EPriority fPriority       thread priority
             TThread::EState fState          thread state
             TThread::EState fStateComing    coming thread state
                      Long_t fId             thread id
                      Long_t fHandle         Win32 thread handle
                      Bool_t fDetached       kTRUE if thread is Detached
                      Bool_t fNamed          kTRUE if thread is Named
                       void* fFcnRetn        void* start function of thread
                       void* fFcnVoid        void  start function of thread
                       void* fThreadArg      thread start function arguments
                       void* fClean          support of cleanup structure
                       void* fTsd[20]        thread specific data container
                        char fComment[100]   thread specific state comment
          static TThreadImp* fgThreadImp     static pointer to thread implementation
                static char* fgXAct          Action name to do by main thread
               static void** volatilefgXArr  pointer to control array of void pointers for action
                static Int_t fgXAnb          size of array above
                static Int_t fgXArt          return XA flag
               static Long_t fgMainId        thread id of main thread
             static TThread* fgMain          pointer to chain of TThread's
              static TMutex* fgMainMutex     mutex to protect chain of threads
              static TMutex* fgXActMutex     mutex to protect XAction
          static TCondition* fgXActCondi     condition for XAction

        public:

          static const TThread::EPriority kLowPriority
          static const TThread::EPriority kNormalPriority
          static const TThread::EPriority kHighPriority
             static const TThread::EState kInvalidState
             static const TThread::EState kNewState
             static const TThread::EState kRunningState
             static const TThread::EState kTerminatedState
             static const TThread::EState kFinishedState
             static const TThread::EState kCancelingState
             static const TThread::EState kCanceledState
             static const TThread::EState kDeletingState

## Class Description

     TThread

     This class implements threads. A thread is an execution environment
     much lighter than a process. A single process can have multiple
     threads. The actual work is done via the TThreadImp class (either
     TPosixThread or TWin32Thread).

TThread(VoidRtnFunc_t fn, void *arg, EPriority pri) : TNamed("<anon>", "")

     Create a thread. Specify the function or static class method
     to be executed by the thread and a pointer to the argument structure.
     The user function should return a void*. To start the thread call Run().

TThread(VoidFunc_t fn, void *arg, EPriority pri) : TNamed("<anon>", "")

     Create a detached thread. Specify the function or class method
     to be executed by the thread and a pointer to the argument structure.
     To start the thread call Run().

TThread(const char *thname, VoidRtnFunc_t fn, void *arg, EPriority pri) : TNamed(thname, "")

     Create thread with a name. Specify the function or class method
     to be executed by the thread and a pointer to the argument structure.
     The user function should return a void*. To start the thread call Run().

TThread(const char *thname, VoidFunc_t fn, void *arg, EPriority pri) : TNamed(thname, "")

     Create a detached thread with a name. Specify the function or class
     method to be executed by the thread and a pointer to the argument
     structure. To start the thread call Run().

TThread(Int_t id)

     Create a TThread for a already running thread.

void Init()

     Initialize global state and variables once.

void Constructor()

     Common thread constructor.

~TThread()

     Cleanup the thread.

Int_t Delete(TThread *th)

     Static method to delete the specified thread.

Int_t Exists()

     Static method to check if threads exist.
     returns the number of running threads.

void SetPriority(EPriority pri)

     Set thread priority.

TThread* GetThread(Long_t id)

     Static method to find a thread by id.

TThread* GetThread(const char *name)

     Static method to find a thread by name.

TThread* Self()

     Static method returning pointer to current thread.

Long_t Join(void **ret)

     Join this thread.

Long_t Join(Long_t jid, void **ret)

     Static method to join a thread by id.

Long_t SelfId()

     Static method returning the id for the current thread.

Int_t Run(void *arg)

     Start the thread. This starts the static method TThread::Function()
     which calls the user function specified in the TThread ctor with
     the arg argument.

Int_t Kill()

     Kill this thread.

Int_t Kill(Long_t id)

     Static method to kill the thread by id.

Int_t Kill(const char *name)

     Static method to kill thread by name.

Int_t SetCancelOff()

     Static method to turn off thread cancellation.

Int_t SetCancelOn()

     Static method to turn on thread cancellation.

Int_t SetCancelAsynchronous()

     Static method to set asynchronous cancellation.

Int_t SetCancelDeferred()

     Static method to set deffered cancellation.

Int_t CancelPoint()

     Static method to set a cancellation point.

Int_t CleanUpPush(void *free, void *arg)

     Static method which pushes thread cleanup method on stack.
     Returns 0 in case of success and -1 in case of error.

Int_t CleanUpPop(Int_t exe)

     Static method which pops thread cleanup method off stack.
     Returns 0 in case of success and -1 in case of error.

Int_t CleanUp()

     Static method to cleanup the calling thread.

void AfterCancel(TThread *th)

     Static method which is called after the thread has been canceled.

Int_t Exit(void *ret)

     Static method which terminates the execution of the calling thread.

Int_t Sleep(ULong_t secs, ULong_t nanos)

     Static method to sleep the calling thread.

Int_t GetTime(ULong_t *absSec, ULong_t *absNanoSec)

     Static method to get the current time. Returns
     the number of seconds.

Int_t Lock()

     Static method to lock the main thread mutex.

Int_t TryLock()

     Static method to try to lock the main thread mutex.

Int_t UnLock()

     Static method to unlock the main thread mutex.

void* Function(void *ptr)

     Static method which is called by the system thread function and
     which in turn calls the actual user function.

void Ps()

     Static method listing the existing threads.

void** Tsd(void *dflt, Int_t k)

     Static method returning a pointer to thread specific data container
     of the calling thread.

void Printf(const char *va_(fmt), ...)

     Static method providing a thread safe printf. Appends a newline.

void ErrorHandler(int level, const char *location, const char *fmt, va_list ap) const

     Thread specific error handler function.
     It calls the user set error handler in the main thread.

void DoError(int level, const char *location, const char *fmt, va_list va) const

     Interface to ErrorHandler. User has to specify the class name as
     part of the location, just like for the global Info(), Warning() and
     Error() functions.

Int_t XARequest(const char *xact, Int_t nb, void **ar, Int_t *iret)

     Static method used to allow commands to be executed by the main thread.

void XAction()

     Static method called via the thread timer to execute in the main
     thread certain commands. This to avoid sophisticated locking and
     possible deadlocking.

### Inline Functions

                          void SetComment(const char* txt = "0")
            TThread::EPriority GetPriority() const
               TThread::EState GetState() const
                        Long_t GetId() const
                          void ps()
                         Int_t Delete(TThread* th)
                       TClass* Class()
                       TClass* IsA() const
                          void ShowMembers(TMemberInspector& insp, char* parent)
                          void Streamer(TBuffer& b)
                          void StreamerNVirtual(TBuffer& b)
                      TThread& operator=(const TThread&)

Author: Fons Rademakers 02/07/97
Last update: root/thread:$Name: $:$Id: TThread.cxx,v 1.38 2005/06/23 20:51:14 rdm Exp $
Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *