nux-1.14.0
nux::NThread Class Reference
Inheritance diagram for nux::NThread:
nux::AbstractThread nux::SystemThread nux::WindowThread

List of all members.

Classes

class  NThreadContext

Public Member Functions

 NThread ()
 NThread (ThreadRoutineFunc lpExternalRoutine)
virtual ~NThread ()
virtual ThreadState Start (void *arg=NULL)
virtual ThreadState Stop (bool bForceKill=false)
ThreadState Suspend ()
ThreadState Resume ()
ThreadState ResumeStart ()
ThreadState ResumeExit ()
t_u32 GetExitCode () const
void Attach (ThreadRoutineFunc lpThreadFunc)
void Detach (void)
t_u32 GetThreadId ()
ThreadState GetThreadState () const
void SetThreadState (ThreadState state)

Protected Member Functions

virtual t_u32 Run (void *)
virtual bool ThreadCtor ()
virtual bool ThreadDtor ()

Static Protected Member Functions

static void * EntryPoint (void *)

Protected Attributes

volatile ThreadState m_ThreadState
NThreadContext m_ThreadCtx
ThreadRoutineFunc m_pThreadFunc

Detailed Description

Definition at line 281 of file ThreadGNU.h.


Constructor & Destructor Documentation

nux::NThread::NThread ( )

Info: Default Constructor

Definition at line 125 of file ThreadGNU.cpp.

References EntryPoint().

    :   m_ThreadState (THREADINIT)
  {
    m_pThreadFunc = NThread::EntryPoint; // Can call Detach() also.
  }
nux::NThread::NThread ( ThreadRoutineFunc  lpExternalRoutine)

Info: Plug Constructor

Use this to migrate/port existing worker threads to objects immediately Although you lose the benefits of ThreadCTOR and ThreadDTOR.

Definition at line 131 of file ThreadGNU.cpp.

References Attach().

  {
    Attach (lpExternalRoutine);
  }
nux::NThread::~NThread ( ) [virtual]

Info: Default Destructor

I think it is wise to destroy the thread even if it is running, when the main thread reaches here.

Definition at line 136 of file ThreadGNU.cpp.

References m_ThreadCtx.

  {
    if (m_ThreadCtx.m_dwTID)
      pthread_detach (m_ThreadCtx.m_dwTID);
  }

Member Function Documentation

void nux::NThread::Attach ( ThreadRoutineFunc  lpThreadFunc) [inline]

Info: Attaches a Thread Function

Used primarily for porting but can serve in developing generic thread objects

Definition at line 338 of file ThreadGNU.h.

Referenced by NThread().

    {
      m_pThreadFunc = lpThreadFunc;
    }
void nux::NThread::Detach ( void  ) [inline]

Info: Detaches the Attached Thread Function

Detaches the Attached Thread Function, If any. by resetting the thread function pointer to EntryPoint1

Definition at line 349 of file ThreadGNU.h.

References EntryPoint().

    {
      m_pThreadFunc = NThread::EntryPoint;
    }
void * nux::NThread::EntryPoint ( void *  pArg) [static, protected]

Info: DONT override this method.

This function is like a standard template. Override if you are sure of what you are doing.

In C++ the entry function of a thread cannot be a normal member function of a class. However, it can be a static member function of a class. This is what we will use as the entry point. There is a gotcha here though. Static member functions do not have access to the this pointer of a C++ object. They can only access static data. Fortunately, there is way to do it. Thread entry point functions take a void * as a parameter so that the caller can typecast any data and pass in to the thread. We will use this to pass this to the static function. The static function will then typecast the void * and use it to call a non static member function

Definition at line 200 of file ThreadGNU.cpp.

References m_ThreadCtx, Run(), ThreadCtor(), and ThreadDtor().

Referenced by Detach(), and NThread().

  {
    NThread *pParent = reinterpret_cast<NThread *> (pArg);

    if (pParent == 0)
    {
      nuxDebugMsg (TEXT ("[NThread::EntryPoint] Invalid pointer. The thread will exit.") );
      return 0;
    }

    if (!pParent->ThreadCtor() )
    {
      // return another message saying the thread could not execute due to error in ThreadCtor;
    }

    pParent->Run ( pParent->m_ThreadCtx.m_pUserData );

    pParent->ThreadDtor();
    return 0;
  }
t_u32 nux::NThread::GetExitCode ( ) const

Info: Starts the thread.

This function starts the thread pointed by m_pThreadFunc with default attributes

Definition at line 221 of file ThreadGNU.cpp.

References m_ThreadCtx.

  {
    return m_ThreadCtx.m_dwExitCode;
  }
virtual t_u32 nux::NThread::Run ( void *  ) [inline, protected, virtual]

Info: Override this method.

This function should contain the body/code of your thread. Notice the signature is similar to that of any worker thread function except for the calling convention.

Reimplemented in nux::AbstractThread, nux::SystemThread, and nux::WindowThread.

Definition at line 385 of file ThreadGNU.h.

References m_ThreadCtx.

Referenced by EntryPoint().

    {
      return m_ThreadCtx.m_dwExitCode;
    }
ThreadState nux::NThread::Start ( void *  arg = NULL) [virtual]

Info: Starts the thread.

This function starts the thread pointed by m_pThreadFunc with default attributes

Reimplemented in nux::SystemThread, and nux::WindowThread.

Definition at line 142 of file ThreadGNU.cpp.

References m_ThreadCtx.

  {
    m_ThreadCtx.m_pUserData = arg;
    int ret = pthread_create (&m_ThreadCtx.m_dwTID,
                              NULL,
                              m_pThreadFunc,
                              this);

    if (ret != 0)
    {
      nuxDebugMsg (TEXT ("[NThread::Start] Cannot start thread.") );
      m_ThreadState = THREAD_START_ERROR;
      return m_ThreadState;
    }

    return m_ThreadState;
  }
ThreadState nux::NThread::Stop ( bool  bForceKill = false) [virtual]

Info: Stops the thread.

This function stops the current thread. We can force kill a thread which results in a TerminateThread.

Definition at line 160 of file ThreadGNU.cpp.

References m_ThreadCtx.

  {
    int ret = pthread_detach (m_ThreadCtx.m_dwTID);

    if (ret != 0)
    {
      nuxDebugMsg (TEXT ("[NThread::Stop] Cannot detach thread.") );
      m_ThreadState = THREAD_STOP_ERROR;
      return m_ThreadState;
    }

    m_ThreadState = THREADSTOP;
    return m_ThreadState;
  }
virtual bool nux::NThread::ThreadCtor ( ) [inline, protected, virtual]

Info: Constructor-like function.

Will be called by EntryPoint before executing the thread body. Override this function to provide your extra initialization.

NOTE: do not confuse it with the classes constructor

Returns:
TRUE if the thread can continue running the program. If FALSE is returned, the thread won't execute the main body Run() and will exit without calling ThreadDtor.

Reimplemented in nux::SystemThread, and nux::WindowThread.

Definition at line 399 of file ThreadGNU.h.

Referenced by EntryPoint().

    {
      return true;
    }
virtual bool nux::NThread::ThreadDtor ( ) [inline, protected, virtual]

Info: Destructor-like function.

Will be called by EntryPoint after executing the thread body. Override this function to provide your extra destruction.

NOTE: do not confuse it with the classes constructor

Returns:
TRUE if this function executed without problems.

Reimplemented in nux::SystemThread, and nux::WindowThread.

Definition at line 413 of file ThreadGNU.h.

Referenced by EntryPoint().

    {
      return true;
    }

Member Data Documentation

NThreadContext nux::NThread::m_ThreadCtx [protected]

Attributes Section

Info: Members of NThread

Definition at line 460 of file ThreadGNU.h.

Referenced by EntryPoint(), GetExitCode(), Run(), Start(), Stop(), and ~NThread().


The documentation for this class was generated from the following files:
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends