Evo C++ Library v0.5.1
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Protected Attributes | List of all members
ThreadClass Struct Referenceabstract

#include <evo/thread.h>

Inheritance diagram for ThreadClass:
Inheritance graph
[legend]

Detailed Description

Base class for managing a single class-based thread of execution.

Example
#include <evo/thread.h>
using namespace evo;
class MyThread : public ThreadClass {
public:
~MyThread() {
}
void thread_run() {
// Thread code
}
};
int main() {
MyThread thread;
thread.thread_start();
thread.thread_cancel_join();
return 0;
}

Public Types

typedef std::function< void(void *)> Func
 Thread function type – with C++11 supports lambda/functor, otherwise just function pointer More...
 

Public Member Functions

 ThreadClass ()
 Constructor. More...
 
virtual ~ThreadClass ()
 Destructor. More...
 
bool cancel_check ()
 Check if thread has been cancelled. More...
 
bool thread_active () const
 Get whether thread is active (running). More...
 
ThreadClassthread_cancel (bool locked=false)
 Set cancel flag to signal thread to stop, and wake thread via condition object. More...
 
bool thread_cancel_join (bool locked=false)
 Cancels and joins thread. More...
 
SysThread::Handle thread_handle () const
 Get platform-specific thread handle. More...
 
bool thread_join ()
 Join thread by waiting for thread to stop. More...
 
virtual void thread_run ()=0
 Thread run method. More...
 
bool thread_start ()
 Start thread. More...
 

Static Public Member Functions

static ulong id ()
 Get current thread ID from system. More...
 
static void yield ()
 Yield control to another thread or process. More...
 

Public Attributes

Condition condmutex
 Condition and mutex object for thread, used to wake up thread. More...
 
Init thread_init
 Thread function pointer More...
 

Protected Attributes

bool thread_active_
 
SysThread thread_impl_
 

Member Typedef Documentation

◆ Func

typedef std::function<void (void*)> Func
inherited

Thread function type – with C++11 supports lambda/functor, otherwise just function pointer

Constructor & Destructor Documentation

◆ ThreadClass()

ThreadClass ( )
inline

Constructor.

◆ ~ThreadClass()

virtual ~ThreadClass ( )
inlinevirtual

Destructor.

  • Derived class destructor should call thread_cancel_join() to stop the thread
  • WARNING: This crashes (calls abort()) if thread is still running – though the running thread may segfault first since at this point the thread object is already partially destroyed

Member Function Documentation

◆ cancel_check()

bool cancel_check ( )
inline

Check if thread has been cancelled.

  • Cancellation is not enforced, the thread has to call cancel_check() regularly to check if it's been cancelled, and stop when cancelled
  • If the thread does not properly stop itself when cancelled, this can hang the parent thread waiting for it
Returns
Whether thread has been cancelled

◆ id()

static ulong id ( )
inlinestaticinherited

Get current thread ID from system.

Returns
Current thread ID

◆ thread_active()

bool thread_active ( ) const
inlineinherited

Get whether thread is active (running).

  • This gets whether the thread was started but hasn't been joined yet – the thread may have terminated though
Returns
Whether active, true if thread has started and hasn't been joined yet

◆ thread_cancel()

ThreadClass& thread_cancel ( bool  locked = false)
inline

Set cancel flag to signal thread to stop, and wake thread via condition object.

  • Cancellation is not enforced, the thread has to call cancel_check() regularly to check if it's been cancelled
  • After setting cancel flag, this notifies on condmutex to wake thread
  • This always unlocks condmutex when done
Parameters
lockedWhether condmutex object is already locked
Returns
This

◆ thread_cancel_join()

bool thread_cancel_join ( bool  locked = false)
inline

Cancels and joins thread.

Parameters
lockedPassed to thread_cancel(): Whether condmutex object is already locked
Returns
Whether successful, false on error (out of memory/resources) or if thread not started

◆ thread_handle()

SysThread::Handle thread_handle ( ) const
inlineinherited

Get platform-specific thread handle.

Returns
Thread handle

◆ thread_join()

bool thread_join ( )
inlineinherited

Join thread by waiting for thread to stop.

  • Class-based thread: May need to call thread_cancel() and wake the thread first, depending on thread implementation
Returns
Whether successful, false on error (out of memory/resources) or if thread not started

◆ thread_run()

virtual void thread_run ( )
pure virtual

Thread run method.

  • This is the thread implementation
  • This should NOT use anything in base class (ThreadClass) beginning with "thread_", otherwise results are undefined
  • Cancellation is not enforced, this has to call cancel_check() regularly to check if it's been cancelled, and if cancelled return ASAP from this method
  • This should NOT block indefinitely without at least checking for cancellation – otherwise this can hang the parent thread

◆ thread_start()

bool thread_start ( )
inlineinherited

Start thread.

  • The thread will start immediately, possibly even before this returns
Returns
Whether successful, false on error (out of memory/resources or thread_init.func=NULL)

◆ yield()

static void yield ( )
inlinestaticinherited

Yield control to another thread or process.

Member Data Documentation

◆ condmutex

Condition condmutex

Condition and mutex object for thread, used to wake up thread.

◆ thread_active_

bool thread_active_
protectedinherited

◆ thread_impl_

SysThread thread_impl_
protectedinherited

◆ thread_init

Init thread_init
inherited

Thread function pointer


The documentation for this struct was generated from the following file: