Jump to content

Programming Reference:Thread Class: Difference between revisions

From BCI2000 Wiki
Mellinger (talk | contribs)
No edit summary
Mellinger (talk | contribs)
 
(10 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(bool createSuspended=false)===
===Thread()===
The thread's constructor's argument determines whether thread execution is started immediately, or whether the thread is initially put into suspended state. By default, the thread is created unsuspended.
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()===
===Suspend()===
Starts thread execution.
Puts the thread into suspended, i.e. halts execution.
===bool Terminate(TimeUtils::Interval = TimeUtils::Forever)===
===Resume()===
Initiates thread termination by setting the <tt>Terminating</tt> property to ''true'', and waits for the thread to terminate.
Resumes thread operation. For threads that have been created with the <tt>createSuspended</tt> argument set to ''true'', <tt>Resume()</tt> will start thread execution.
If a finite timeout is specified, and it expires, it will return with a result of ''false''. Otherwise, it will return ''true''.
===Terminate()===
Initiates thread termination by setting the <tt>Terminating</tt> property to ''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 31: 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==
===int ID (r)===
===bool Terminating (r)===
The thread's OS thread id, or zero if the thread is not running.
===bool IsTerminating (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