Jump to content

Programming Reference:Thread Class: Difference between revisions

From BCI2000 Wiki
Mellinger (talk | contribs)
Mellinger (talk | contribs)
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
==Location==
==Location==
<tt>BCI2000/src/shared/utils</tt>
<tt>BCI2000/src/shared/utils/Lib</tt>


==Synopsis==
==Synopsis==
The <tt>OSThread</tt> class provides a wrapper for OS-dependent thread functionality.
The <tt>Thread</tt> class provides a wrapper for OS-dependent thread functionality. Differently from <tt>std::thread</tt>, it works together with the <tt>WaitableEvent</tt> class, such that waits are automatically aborted when the thread's <tt>Terminate()</tt> function is called.


==Methods==
==Methods==
===OSThread()===
===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()===
===~OSThread()===
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.
Deleting an <tt>OSThread</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>IsTerminated</tt> property is ''true'' before deleting the <tt>OSThread</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 Execute===
===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::Execute()
int MyThread::OnExecute()
{
{
   while( !IsTerminating() )
   while (!Terminating())
   { // do something
   { // do something
     ...
     ...
Line 29: Line 29:
}
}
</pre>
</pre>
When using a <tt>WaitableEvent</tt> object, the loop may look like this:
<pre>
int MyThread::OnExecute()
{
  while (myEvent.Wait())
  { // do something whenever the event gets signaled
    ...
  }
  return 0;
}
</pre>
==Properties==
==Properties==
===bool IsTerminating (r)===
===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 IsTerminated (r)===
===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>Execute()</tt> function on termination of the thread.
The return code of the thread's <tt>OnExecute()</tt> function on termination of the thread.
 
[[Programming Reference:WaitableEvent Class]]


[[Category:Framework API]][[Category:Development]]
[[Category:Framework API]][[Category:Development]]

Latest revision as of 15:22, 18 April 2023

Location

BCI2000/src/shared/utils/Lib

Synopsis

The Thread class provides a wrapper for OS-dependent thread functionality. Differently from std::thread, it works together with the WaitableEvent class, such that waits are automatically aborted when the thread's Terminate() function is called.

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;
}

When using a WaitableEvent object, the loop may look like this:

int MyThread::OnExecute()
{
  while (myEvent.Wait())
  { // do something whenever the event gets signaled
    ...
  }
  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.

Programming Reference:WaitableEvent Class