OWLNext 7.0
Borland's Object Windows Library for the modern age
|
TThread provides a system-independent interface to threads. More...
#include <owl/thread.h>
Classes | |
class | TThreadError |
The error class that defines the objects that are thrown when an error occurs. More... | |
Public Types | |
enum | { NoLimit = INFINITE } |
enum | TCurrent { Current } |
enum | TStatus { Created , Running , Suspended , Finished , Invalid } |
Identifies the states that the class can be in. More... | |
enum | TPriority { Idle = THREAD_PRIORITY_IDLE , Lowest = THREAD_PRIORITY_LOWEST , BelowNormal = THREAD_PRIORITY_BELOW_NORMAL , Normal = THREAD_PRIORITY_NORMAL , AboveNormal = THREAD_PRIORITY_ABOVE_NORMAL , Highest = THREAD_PRIORITY_HIGHEST , TimeCritical = THREAD_PRIORITY_TIME_CRITICAL } |
typedef HANDLE | THandle |
typedef TStatus | Status |
For compatibility with old T-less typenames. | |
typedef TThreadError | ThreadError |
Public Member Functions | |
TThread (TCurrent) | |
Attach to a running thread. | |
THandle | Start () |
Starts the thread executing. | |
ulong | Suspend () |
Suspends execution of the thread. | |
ulong | Resume () |
Resumes execution of a suspended thread. | |
bool | Sleep (long timeMS, bool alertable=false) |
Suspends execution of the current thread for the number of milliseconds specified by the timeMS parameter. | |
virtual void | Terminate () |
Mark the thread for termination. | |
ulong | WaitForExit (ulong timeout=NoLimit) |
Blocks the calling thread until the internal thread exits or until the time specified by timeout, in milliseconds,expires. | |
ulong | TerminateAndWait (ulong timeout=NoLimit) |
Combines the behavior of Terminate() and WaitForExit(). | |
TStatus | GetStatus () const |
Gets the current status of the thread. | |
uint32 | GetExitCode () const |
int | GetPriority () const |
Gets the thread priority. | |
int | SetPriority (int) |
Can pass a TPriority for simplicity. | |
Protected Member Functions | |
TThread () | |
Create a thread. Derived class overrides Run() | |
TThread (const TThread &) | |
Puts the object into the Created state, just like the default constructor. | |
const TThread & | operator= (const TThread &) |
TThread assignment operator. | |
virtual | ~TThread () |
TThread destructor. | |
bool | ShouldTerminate () const |
Returns a bool value to indicate that Terminate() or TerminateAndWait() has been called. | |
void | Exit (ulong code) |
Alternative to returning from Run() | |
TThread provides a system-independent interface to threads.
With suitable underlying implementations the same code can be used under OS/2 and Win32.
Example
Internal States
When the user calls Suspend() the object moves into the Suspended state. When the thread exits the object moves into the Finished state.Calling Resume() on an object that is in the Running state is an error and will throw an exception.
Identifies the states that the class can be in.
owl::TThread::TThread | ( | TCurrent | ) |
Attach to a running thread.
Definition at line 411 of file thread.cpp.
|
protected |
Create a thread. Derived class overrides Run()
Creates an object of type TThread.
Definition at line 398 of file thread.cpp.
|
protected |
Puts the object into the Created state, just like the default constructor.
Does not copy any of the internal details of the thread being copied.
Definition at line 429 of file thread.cpp.
|
protectedvirtual |
TThread destructor.
If the thread hasn't finished, destroying its control object is an error.
Definition at line 482 of file thread.cpp.
References _T, owl::TThread::TThreadError::DestroyBeforeExit, GetStatus(), Running, Suspended, and WARN.
Alternative to returning from Run()
Exit provides an alternative to returning from Run.
Called from within the thread that wants to exit early.
Definition at line 677 of file thread.cpp.
References Finished.
|
inline |
|
inline |
TThread assignment operator.
Used when assigning derived objects. Attempting to assign from a running object is an error, since the data fields in the running object can be changing asynchronously.
The target object must be in either the Created state or the Finished state. If so, puts the object into the Created state. If the object is not in either the Created state or the Finished state it is an error and an exception will be thrown.
Definition at line 454 of file thread.cpp.
References owl::TThread::TThreadError::AssignError, Created, Finished, GetStatus(), and Suspended.
ulong owl::TThread::Resume | ( | ) |
Resumes execution of a suspended thread.
It's an error to try to resume a thread that isn't suspended.
Definition at line 558 of file thread.cpp.
References _T, Created, Finished, GetStatus(), owl::TThread::TThreadError::ResumeAfterExit, owl::TThread::TThreadError::ResumeBeforeRun, owl::TThread::TThreadError::ResumeDuringRun, Running, and TRACEX.
Can pass a TPriority for simplicity.
Sets the priority of the thread.
Definition at line 625 of file thread.cpp.
|
inlineprotected |
Returns a bool value to indicate that Terminate() or TerminateAndWait() has been called.
If this capability is being used, the thread should call ShouldTerminate() regularly, and if it returns a non-zero value the thread finish its processing and exit.
Suspends execution of the current thread for the number of milliseconds specified by the timeMS parameter.
If the alertable parameter is set to true, the function resumes execution upon I/O completion.
The function returns true if its wakeup is due to I/O completion.
TThread::THandle owl::TThread::Start | ( | ) |
Starts the thread executing.
The actual call depends on the operating system. Returns the handle of the thread. After the system call we check status.
Definition at line 502 of file thread.cpp.
References _T, Created, owl::TThread::TThreadError::CreationFailure, GetStatus(), Invalid, Running, and TRACEX.
ulong owl::TThread::Suspend | ( | ) |
Suspends execution of the thread.
It's an error to try to suspend a thread that hasn't been started or that has already terminated.
Definition at line 535 of file thread.cpp.
References _T, Created, Finished, GetStatus(), owl::TThread::TThreadError::SuspendAfterExit, owl::TThread::TThreadError::SuspendBeforeRun, Suspended, and TRACEX.
|
virtual |
Mark the thread for termination.
Sets an internal flag that indicates that the thread should exit. The derived class can check the state of this flag by calling ShouldTerminate().
Definition at line 586 of file thread.cpp.
Combines the behavior of Terminate() and WaitForExit().
Sets an internal flag that indicates that the thread should exit and blocks the calling thread until the internal thread exits or until the time specified by timeout, in milliseconds, expires. A timeout of NoLimit says to wait indefinitely.
Definition at line 616 of file thread.cpp.
References Terminate(), and WaitForExit().
Blocks the calling thread until the internal thread exits or until the time specified by timeout, in milliseconds,expires.
A timeout of NoLimit says to wait indefinitely.
Definition at line 598 of file thread.cpp.