Back to index

nux  3.0.0
Classes | Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Private Member Functions
nux::NThread Class Reference

#include <ThreadGNU.h>

Inheritance diagram for nux::NThread:
Inheritance graph
[legend]
Collaboration diagram for nux::NThread:
Collaboration graph
[legend]

List of all members.

Classes

class  NThreadContext
 Info: Thread Context Inner Class. More...

Public Member Functions

 NThread ()
 Info: Default Constructor.
 NThread (ThreadRoutineFunc lpExternalRoutine)
 Info: Plug Constructor.
virtual ~NThread ()
 Info: Default Destructor.
virtual ThreadState Start (void *arg=NULL)
 Info: Starts the thread.
virtual ThreadState Stop (bool bForceKill=false)
 Info: Stops the thread.
ThreadState Suspend ()
ThreadState Resume ()
ThreadState ResumeStart ()
ThreadState ResumeExit ()
unsigned int GetExitCode () const
 Info: Starts the thread.
void Attach (ThreadRoutineFunc lpThreadFunc)
 Info: Attaches a Thread Function.
void Detach (void)
 Info: Detaches the Attached Thread Function.
pthread_t GetPThread ()
ThreadState GetThreadState () const
void SetThreadState (ThreadState state)

Static Public Member Functions

static ThreadWaitResult JoinThread (NThread *thread, unsigned int milliseconds)
 Wait for a thread to complete.

Protected Member Functions

virtual int Run (void *)
 Info: Override this method.
virtual bool ThreadCtor ()
 Info: Constructor-like function.
virtual bool ThreadDtor ()
 Info: Destructor-like function.

Static Protected Member Functions

static void * EntryPoint (void *)
 Info: DONT override this method.

Protected Attributes

volatile ThreadState m_ThreadState
NThreadContext m_ThreadCtx
 Attributes Section.
ThreadRoutineFunc m_pThreadFunc

Private Member Functions

 NUX_DECLARE_ROOT_OBJECT_TYPE (NThread)

Detailed Description

Definition at line 295 of file ThreadGNU.h.


Constructor & Destructor Documentation

Info: Default Constructor.

Definition at line 124 of file ThreadGNU.cpp.

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

Here is the call graph for this function:

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 130 of file ThreadGNU.cpp.

  {
    Attach (lpExternalRoutine);
  }

Here is the call graph for this function:

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 135 of file ThreadGNU.cpp.

  {
    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 352 of file ThreadGNU.h.

    {
      m_pThreadFunc = lpThreadFunc;
    }

Here is the caller graph for this function:

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 363 of file ThreadGNU.h.

Here is the call graph for this function:

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 199 of file ThreadGNU.cpp.

  {
    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;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

unsigned int nux::NThread::GetExitCode ( ) const

Info: Starts the thread.

This function starts the thread pointed by m_pThreadFunc with default attributes

Definition at line 220 of file ThreadGNU.cpp.

pthread_t nux::NThread::GetPThread ( )

Definition at line 225 of file ThreadGNU.cpp.

  {
    return m_ThreadCtx.m_dwTID;
  }

Here is the caller graph for this function:

Definition at line 230 of file ThreadGNU.cpp.

  {
    return m_ThreadState;
  }

Here is the caller graph for this function:

ThreadWaitResult nux::NThread::JoinThread ( NThread thread,
unsigned int  milliseconds 
) [static]

Wait for a thread to complete.

The second parameters to this call specifies a how long to wait for the thread to complete. The following options are available: ThreadWaitTimeout::THREAD_WAIT_TIMEOUT_NONE: The function returns immediately if the thread exits. ThreadWaitTimeout::THREAD_WAIT_TIMEOUT_FOREVER: The function waits until the thread exits.

Parameters:
threadPointer to a valid NThread.
millisecondsTime to wait for the thread to complete.

Definition at line 240 of file ThreadGNU.cpp.

  {
    if (thread == NULL)
    {
      return THREAD_WAIT_RESULT_FAILED;
    }

    void *value_ptr = NULL;
    int result = pthread_join(thread->GetPThread(), &value_ptr);

    switch(result)
    {
    case 0:
      return THREAD_WAIT_RESULT_COMPLETED;
    default:
      return THREAD_WAIT_RESULT_FAILED;
    }
  }

Here is the call graph for this function:

Definition at line 180 of file ThreadGNU.cpp.

  {
    return m_ThreadState;
  }

Definition at line 193 of file ThreadGNU.cpp.

Definition at line 186 of file ThreadGNU.cpp.

virtual int 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::WindowThread, nux::AbstractThread, and nux::SystemThread.

Definition at line 410 of file ThreadGNU.h.

Here is the caller graph for this function:

Definition at line 235 of file ThreadGNU.cpp.

  {
    m_ThreadState = state;
  }

Here is the caller graph for this function:

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::WindowThread, and nux::SystemThread.

Definition at line 141 of file ThreadGNU.cpp.

  {
    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;
  }

Here is the caller graph for this function:

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 159 of file ThreadGNU.cpp.

  {
    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;
  }

Here is the caller graph for this function:

Definition at line 174 of file ThreadGNU.cpp.

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::WindowThread, and nux::SystemThread.

Definition at line 424 of file ThreadGNU.h.

    {
      return true;
    }

Here is the caller graph for this function:

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::WindowThread, and nux::SystemThread.

Definition at line 438 of file ThreadGNU.h.

    {
      return true;
    }

Here is the caller graph for this function:


Member Data Documentation

Definition at line 486 of file ThreadGNU.h.

Attributes Section.

Info: Members of NThread

Definition at line 485 of file ThreadGNU.h.

Definition at line 386 of file ThreadGNU.h.


The documentation for this class was generated from the following files: