Jump to content

Programming Reference:Thread Class: Difference between revisions

From BCI2000 Wiki
Mellinger (talk | contribs)
No edit summary
Mellinger (talk | contribs)
No edit summary
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
<!--
TODO V3
update to V3 OSThread interface
-->
==Location==
==Location==
<tt>BCI2000/src/shared/utils</tt>
<tt>BCI2000/src/shared/utils</tt>
Line 10: Line 6:


==Methods==
==Methods==
===OSThread(bool createSuspended=false)===
===OSThread()===
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.
 
===~OSThread()===
===~OSThread()===
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.
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.
===Suspend()===
===Start()===
Puts the thread into suspended, i.e. halts execution.
Starts thread execution.
===Resume()===
===Terminate([OSEvent*])===
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.
Initiates thread termination by setting the <tt>Terminating</tt> property to ''true''. When a pointer to an [[Programming Reference::OSEvent Class|<tt>OSEvent</tt>]] object is specified as an argument, the  [[Programming Reference::OSEvent Class|<tt>OSEvent</tt>]] object will be set to signaled state as soon as thread execution has terminated.
===Terminate()===
 
Initiates thread termination by setting the <tt>Terminating</tt> property to ''true''.
==Static Methods==
===Sleep( int ms )===
Puts the current thread to sleep for the amount of time specified in ms.


==Events==
==Events==
Line 36: Line 33:
</pre>
</pre>
==Properties==
==Properties==
===int ID (r)===
The thread's OS thread id, or zero if the thread is not running.
===bool IsTerminating (r)===
===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.
Line 44: Line 39:
===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>Execute()</tt> function on termination of the thread.
[[Programming Reference:OSEvent Class]], [[Programming Reference:OSMutex Class]]


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

Revision as of 14:49, 2 May 2011

Location

BCI2000/src/shared/utils

Synopsis

The OSThread class provides a wrapper for OS-dependent thread functionality.

Methods

OSThread()

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

~OSThread()

Deleting an OSThread object with a running thread associated will kill the thread, resulting in an undefined state of the parent process. Make sure that the IsTerminated property is true before deleting the OSThread object.

Start()

Starts thread execution.

Terminate([OSEvent*])

Initiates thread termination by setting the Terminating property to true. When a pointer to an OSEvent object is specified as an argument, the OSEvent object will be set to signaled state as soon as thread execution has terminated.

Static Methods

Sleep( int ms )

Puts the current thread to sleep for the amount of time specified in ms.

Events

int Execute

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::Execute()
{
  while( !IsTerminating() )
  { // do something
    ...
  }
  return 0;
}

Properties

bool IsTerminating (r)

True after Terminate() has been called on the thread.

bool IsTerminated (r)

True after the thread has been terminated.

int Result (r)

The return code of the thread's Execute() function on termination of the thread.

Programming Reference:OSEvent Class, Programming Reference:OSMutex Class