TrinityCore
|
#include <GThread.h>
Public Types | |
enum | { NUM_CORES = -100 } |
typedef shared_ptr< class GThread > | Ref |
Public Member Functions | |
GThread (const std::string &name) | |
virtual | ~GThread () |
bool | start (SpawnBehavior behavior=USE_NEW_THREAD) |
void | terminate () |
bool | running () const |
bool | started () const |
bool | completed () const |
void | waitForCompletion () |
const std::string & | name () |
Public Member Functions inherited from G3D::ReferenceCountedObject | |
virtual | ~ReferenceCountedObject () |
Static Public Member Functions | |
static int | numCores () |
static GThreadRef | create (const std::string &name, void(*proc)(void *), void *param=NULL) |
template<class Class > | |
static void | runConcurrently2D (const Vector2int32 &start, const Vector2int32 &upTo, Class *object, void(Class::*method)(int x, int y), int maxThreads=NUM_CORES) |
Iterates over a 2D region using multiple threads and blocks until all threads have completed. More... | |
template<class Class > | |
static void | runConcurrently2D (const Vector2int32 &start, const Vector2int32 &upTo, Class *object, void(Class::*method)(int x, int y, int threadID), int maxThreads=NUM_CORES) |
Static Public Attributes | |
static const SpawnBehavior | USE_CURRENT_THREAD = G3D::USE_CURRENT_THREAD |
static const SpawnBehavior | USE_NEW_THREAD = G3D::USE_NEW_THREAD |
Protected Member Functions | |
virtual void | threadMain ()=0 |
Private Types | |
enum | GStatus { STATUS_CREATED, STATUS_STARTED, STATUS_RUNNING, STATUS_COMPLETED } |
Private Member Functions | |
GThread (const GThread &) | |
GThread & | operator= (const GThread &) |
bool | operator== (const GThread &) |
Static Private Member Functions | |
static void * | internalThreadProc (void *param) |
Private Attributes | |
volatile GStatus | m_status |
pthread_t | m_handle |
std::string | m_name |
Platform independent thread implementation. You can either subclass and override GThread::threadMain or call the create method with a method.
Beware of reference counting and threads. If circular references exist between GThread subclasses then neither class will ever be deallocated. Also, dropping all pointers (and causing deallocation) of a GThread does NOT stop the underlying process.
typedef shared_ptr<class GThread> G3D::GThread::Ref |
anonymous enum |
Enumerator | |
---|---|
NUM_CORES |
Tells GThread::runConcurrently() and GThread::runConcurrently2D() to use System::numCores() threads. |
|
private |
G3D::GThread::GThread | ( | const std::string & | name | ) |
|
virtual |
bool G3D::GThread::completed | ( | ) | const |
Returns true if the thread has exited.
|
static |
Constructs a basic GThread without requiring a subclass.
proc | The global or static function for the threadMain() |
|
staticprivate |
|
inline |
Returns thread name
|
static |
Returns System::numCores(); put here to break a dependence on System.h
|
inlinestatic |
Iterates over a 2D region using multiple threads and blocks until all threads have completed.
Evaluates object->method(x, y) for every start.x <= x < upTo.x
and start.y <= y < upTo.y
. Iteration is row major, so each thread can expect to see successive x values.
maxThreads | Maximum number of threads to use. By default at most one thread per processor core will be used. |
Example:
|
inlinestatic |
Like the other version of runConcurrently2D, but tells the method the thread index that it is running on. That enables the caller to manage per-thread state.
bool G3D::GThread::running | ( | ) | const |
Returns true if threadMain is currently executing. This will only be set when the thread is actually running and might not be set when start() returns.
bool G3D::GThread::start | ( | SpawnBehavior | behavior = USE_NEW_THREAD | ) |
Starts the thread and executes threadMain(). Returns false if the thread failed to start (either because it was already started or because the OS refused).
behavior | If USE_CURRENT_THREAD, rather than spawning a new thread, this routine runs threadMain on the current thread. |
bool G3D::GThread::started | ( | ) | const |
True after start() has been called, even through the thread may have already completed(), or be currently running().
void G3D::GThread::terminate | ( | ) |
Terminates the thread without notifying or waiting for a cancelation point.
|
protectedpure virtual |
Overriden by the thread implementor
Implemented in G3D::_internalGThreadWorker< Class >, and G3D::_internal::BasicThread.
void G3D::GThread::waitForCompletion | ( | ) |
Waits for the thread to finish executing.
|
private |
|
private |
|
private |
|
static |
For backwards compatibility to G3D 8.xx
|
static |
For backwards compatibility to G3D 8.xx