Programming Reference:Thread Class

From BCI2000 Wiki
Revision as of 15:22, 18 April 2023 by Mellinger (talk | contribs) (→‎int OnExecute)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search




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.



All threads are created in suspended state, and resumed by calling their Start() function.


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.


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.


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;


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