OSAPIThread Class

The OSAPIThread is used to run code in a new thread. More...

Header: #include <OSAPI/Process/OSAPIThread.h>
Inherits: OSAPITask

Public Functions

virtual ~OSAPIThread()
virtual void Start(CDP::ThreadPriority priority, const char *name, int stackSize = 0)
  • 14 public functions inherited from OSAPITask

Protected Functions

virtual void Main() = 0

Detailed Description

The OSAPIThread is used to run code in a new thread.

Usage

  • To create a thread, make a subclass of OSAPIThread and implement the virtual void Main() function in the subclass.
  • To start the thread, call the Start() function.
  • To stop the thread, call Stop() function.
  • To free all resources allocated for the thread, call Delete() function.

Typical usage in a Component is to override the CDPComponent::Activate() and call OSAPIThread::Start() from there:

void MyComponent::Activate()
{
  CDPComponent::Activate(); // call base
  Start(CDPTHREAD_PRIORITY_HIGH,ShortName());
}

Also override CDPComponent::Suspend():

void MyComponent::Suspend()
{
  CDPComponent::Suspend();
  Stop();
  // trigger any blocking objects here, f.i. call Set() on OSAPIEvent to release a Wait().
  Delete(); // delete will block until thread is no longer running (for max. 2 seconds)
}

A typical Main() function:

void MyComponent::Main()
{
  while(!Stopped())
  {
    m_event.Wait();
    m_event.Reset();
    if(!Stopped())
      DoBlockingOperation();
  }
}

See also OSAPITask and CDPComponent.

Member Function Documentation

[virtual] OSAPIThread::~OSAPIThread()

Stops the running thread.

[pure virtual protected] void OSAPIThread::Main()

This Main() function will be called by the new thread when it has been created by Start().

Override this in your subclass and put your thread code in it. Your implementation of Main() should return if Stopped() returns true.

[virtual] void OSAPIThread::Start(CDP::ThreadPriority priority, const char *name, int stackSize = 0)

Start thread execution.

Parameter priorityThread priority level.

© CDP Technologies AS - All rights reserved