GCGlib  0.04.228
GCG Graphics Engine
gcgTHREAD Class Referenceabstract

Generic abstract class for defining a thread. More...

#include <gcg.h>

Inheritance diagram for gcgTHREAD:
gcgCLASS

Public Member Functions

 gcgTHREAD ()
 Constructs a valid object. No thread is created and method run() is not called. More...
 
virtual ~gcgTHREAD ()
 Deletes the object, releasing its resources. If any thread is running, it calls waitThread() to ask them to finish and wait their exit. The deletion is blocked if run() does not return. Before deleting an gcgTHREAD object, it is a good practice to ask run() to exit normally (synchronously). More...
 
bool startThread ()
 Starts a new control thread that executes gcgTHREAD:run(). It returns immediately to the calling process. If a thread is still running for this instance, returns an error. More...
 
bool abortThread ()
 
bool waitThread ()
 
bool isRunning ()
 
virtual void run ()=0
 
- Public Member Functions inherited from gcgCLASS
void * operator new (size_t size)
 Defines a new operator to be used by instatiations of GCGlib classes instead the global one. More...
 
void * operator new (size_t size, const std::nothrow_t &) throw ()
 Defines a new operator to be used by instantiations of GCGlib classes instead the global one. Returns a NULL pointer instead of throwing an exception if an error occurs. More...
 
void * operator new[] (size_t size)
 Defines a new operator to be used by GCGlib array allocations instead the global one. More...
 
void * operator new[] (size_t size, const std::nothrow_t &) throw ()
 Defines a new operator to be used by vector allocations instead the global one. More...
 
void operator delete (void *p)
 Defines a delete operator to free instances of GCGlib classes instead the global one. It is designed to match the new operator. More...
 
void operator delete (void *p, const std::nothrow_t &) throw ()
 Defines a delete operator to free instances of GCGlib classes instead the global one. It is designed to match the new operator. More...
 
void operator delete[] (void *p)
 Defines a delete operator to free instances of arrays for GCGlib classes instead the global one. It is designed to match the new[] operator. More...
 
void operator delete[] (void *p, const std::nothrow_t &) throw ()
 Defines a delete operator to free instances of arrays for GCGlib classes instead the global one. It is designed to match the new[] operator. More...
 

Public Attributes

void * handle
 Internal handle of this object.
 

Detailed Description

Generic abstract class for defining a thread.

A specialization of this abstract class must implement the method gcgTHREAD::run(). After the invocation of the method gcgTHREAD::startThread(), a control thread is created with pthread library, which calls the run() method. This control thread is destroyed when run() returns, and startThread() can be invoked again. While the run() does not return, or is asynchronously aborted with gcgTHREAD::abortThread(), calling startThread() will result in an error. The thread cancellation with abortThread() must be avoided. It is not a good practice to force the stop of a thread. This may be the source of several problems like memory leaking, deadlocks and data loss. It is preferable to implement cancellation points in the method run(), which can be enabled to safely finish and make run() to return. In this case, use method gcgTHREAD::waitThread() to wait until run() exits. At any time, the user can probe if the thread is running with gcgTHREAD::isRunning().

The method abortThread() stops the control thread by calling pthread_cancel(). Due to system incompatibilities, this functionality does not work asynchronously in Windows systems. This means that abortThread() will not stop the thread immediately but flag the thread to stop when it reaches a pthread cancellation point. If run() does not use threads that call pthreads, it will continue to run after abortThread() returns true. The programmer should avoid this method as much as possible in Windows systems.

Since
0.01.6

Constructor & Destructor Documentation

◆ gcgTHREAD()

gcgTHREAD::gcgTHREAD ( )

Constructs a valid object. No thread is created and method run() is not called.

See also
startThread()

◆ ~gcgTHREAD()

virtual gcgTHREAD::~gcgTHREAD ( )
virtual

Deletes the object, releasing its resources. If any thread is running, it calls waitThread() to ask them to finish and wait their exit. The deletion is blocked if run() does not return. Before deleting an gcgTHREAD object, it is a good practice to ask run() to exit normally (synchronously).

See also
abortThread()
waitThread()

Member Function Documentation

◆ abortThread()

bool gcgTHREAD::abortThread ( )

The method abortThread() forces the thread stop by calling pthread_cancel(). After its return, a new thread can be issued with startThread().This is not a good practice because the thread aborted may leave memory leaks, locks and unsaved data. Avoid this method using user defined cancellation points inside run() that flags it to safely exit. In this case, the thread stop can be checked using waitThread(). Due to system incompatibilities, this funcionality does not work assynchronously in Windows systems. This means that abortThread() will not stop the thread immediately but flag the thread to stop when it reaches a pthread cancelation point. If run() does not use threads that call pthread functions, it will continue to run after abortThread() returns successfully. The programmer should avoid this method as much as possible in Windows systems. In Linux systems, this method can be called even inside the thread.

Returns
true if the thread is succesfully aborted (or signaled to abort). If it returns false, check GCG_REPORT_MESSAGE(gcgGetReport()) for knowing the problem.
See also
startThread()
waitThread()

◆ isRunning()

bool gcgTHREAD::isRunning ( )

Checks if the thread is running. Returns immediately.

Returns
true if the thread is still running.
See also
startThread()

◆ run()

virtual void gcgTHREAD::run ( )
pure virtual

Pure virtual method that is called as a separate thread when startThread() is invoked. It must be implemented by specialized classes.

See also
startThread()
waitThread()
abortThread()
isRunning()

◆ startThread()

bool gcgTHREAD::startThread ( )

Starts a new control thread that executes gcgTHREAD:run(). It returns immediately to the calling process. If a thread is still running for this instance, returns an error.

Returns
true if the thread is succesfully created. If it returns false, check GCG_REPORT_MESSAGE(gcgGetReport()) for knowing the problem.
See also
abortThread()
waitThread()

◆ waitThread()

bool gcgTHREAD::waitThread ( )

This method blocks until the thread execution performed by run() exits or is cancelled. If the thread is not running or is already stoped, it returns immediately. This method can not be called inside the thread to avoid deadlock.

Returns
true if the thread has succesfully exited or cancelled. If it returns false, check GCG_REPORT_MESSAGE(gcgGetReport()) for knowing the problem.
See also
startThread()
abortThread()

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