Programming Reference:Thread Class: Difference between revisions
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
==Location== | ==Location== | ||
<tt>BCI2000/src/shared/utils</tt> | <tt>BCI2000/src/shared/utils/Lib</tt> | ||
==Synopsis== | ==Synopsis== | ||
The <tt> | The <tt>Thread</tt> class provides a wrapper for OS-dependent thread functionality. | ||
==Methods== | ==Methods== | ||
=== | ===Thread()=== | ||
All threads are created in suspended state, and resumed by calling their ''Start()'' function. | All threads are created in suspended state, and resumed by calling their ''Start()'' function. | ||
===~ | ===~Thread()=== | ||
Deleting an <tt> | Deleting an <tt>Thread</tt> object with a running thread associated will kill the thread, resulting in an undefined state of the parent process. Make sure that the <tt>Terminated</tt> property is ''true'' before deleting the <tt>Thread</tt> object. | ||
===Start()=== | ===Start()=== | ||
Starts thread execution. | Starts thread execution. | ||
===Terminate( | ===bool Terminate(TimeUtils::Interval = TimeUtils::Forever)=== | ||
Initiates thread termination by setting the <tt>Terminating</tt> property to ''true''. | Initiates thread termination by setting the <tt>Terminating</tt> property to ''true'', and waits for the thread to terminate. | ||
If a finite timeout is specified, and it expires, it will return with a result of ''false''. Otherwise, it will return ''true''. | |||
==Events== | ==Events== | ||
===int | ===int OnExecute=== | ||
This is the thread's main function. Typically, it will consist of a processing loop that exits when the thread's <tt>IsTerminating</tt> property becomes ''true'': | This is the thread's main function. Typically, it will consist of a processing loop that exits when the thread's <tt>IsTerminating</tt> property becomes ''true'': | ||
<pre> | <pre> | ||
int MyThread:: | int MyThread::OnExecute() | ||
{ | { | ||
while( ! | while( !Terminating() ) | ||
{ // do something | { // do something | ||
... | ... | ||
| Line 33: | Line 30: | ||
</pre> | </pre> | ||
==Properties== | ==Properties== | ||
===bool | ===bool Terminating (r)=== | ||
True after <tt>Terminate()</tt> has been called on the thread. | True after <tt>Terminate()</tt> has been called on the thread. | ||
===bool | ===bool Terminated (r)=== | ||
True after the thread has been terminated. | True after the thread has been terminated. | ||
===int Result (r)=== | ===int Result (r)=== | ||
The return code of the thread's <tt> | The return code of the thread's <tt>OnExecute()</tt> function on termination of the thread. | ||
[[Programming Reference: | [[Programming Reference:WaitableEvent Class]] | ||
[[Category:Framework API]][[Category:Development]] | [[Category:Framework API]][[Category:Development]] | ||
Revision as of 15:11, 18 April 2023
Location
BCI2000/src/shared/utils/Lib
Synopsis
The Thread class provides a wrapper for OS-dependent thread functionality.
Methods
Thread()
All threads are created in suspended state, and resumed by calling their Start() function.
~Thread()
Deleting an Thread object with a running thread associated will kill the thread, resulting in an undefined state of the parent process. Make sure that the Terminated property is true before deleting the Thread object.
Start()
Starts thread execution.
bool Terminate(TimeUtils::Interval = TimeUtils::Forever)
Initiates thread termination by setting the Terminating property to true, and waits for the thread to terminate. If a finite timeout is specified, and it expires, it will return with a result of false. Otherwise, it will return true.
Events
int OnExecute
This is the thread's main function. Typically, it will consist of a processing loop that exits when the thread's IsTerminating property becomes true:
int MyThread::OnExecute()
{
while( !Terminating() )
{ // do something
...
}
return 0;
}
Properties
bool Terminating (r)
True after Terminate() has been called on the thread.
bool Terminated (r)
True after the thread has been terminated.
int Result (r)
The return code of the thread's OnExecute() function on termination of the thread.