TSDuck Version 3.15-964 (TSDuck - The MPEG Transport Stream Toolkit)
ts::Thread Class Referenceabstract

Base class for threads. More...

Inheritance diagram for ts::Thread:

Classes

class  ThreadError
 Fatal low-level threading error. More...
 

Public Member Functions

 Thread ()
 Default constructor (all attributes have their default values). More...
 
 Thread (const ThreadAttributes &attributes)
 Constructor from specified attributes. More...
 
virtual ~Thread ()
 Destructor. More...
 
void getAttributes (ThreadAttributes &attributes)
 Get a copy of the attributes of the thread. More...
 
bool isCurrentThread () const
 Check if the caller is running in the context of this thread. More...
 
virtual void main ()=0
 This hook is invoked in the context of the thread. More...
 
bool setAttributes (const ThreadAttributes &attributes)
 Set new attributes to the thread. More...
 
bool start ()
 Start the thread. More...
 
bool waitForTermination ()
 Wait for thread termination. More...
 

Static Public Member Functions

static void Yield ()
 Yield execution of the current thread. More...
 

Detailed Description

Base class for threads.

This is a base class for threads. A thread object is typically implemented as a subclass of ts::Thread. The code to be executed in the thread shall be implemented in the method main().

This class implements operating system threads. Its implementation is operating system dependent.

Constructor & Destructor Documentation

◆ Thread() [1/2]

ts::Thread::Thread ( )

Default constructor (all attributes have their default values).

See also
ThreadAttributes

◆ Thread() [2/2]

ts::Thread::Thread ( const ThreadAttributes attributes)

Constructor from specified attributes.

For convenience, all setters of the ThreadAttributes class return a reference to the ThreadAttributes object. Thus, it is possible to build a Thread with selected attributes in one shot, using the following method:

class MyThread: public Thread {...};
MyThread thread (ThreadAttributes().setStackSize(XX).setPriority(YY));
Parameters
[in]attributesThe set of attributes.

◆ ~Thread()

virtual ts::Thread::~Thread ( )
virtual

Destructor.

See also
waitForTermination()

Member Function Documentation

◆ setAttributes()

bool ts::Thread::setAttributes ( const ThreadAttributes attributes)

Set new attributes to the thread.

New attributes can be set as long as the thread is not started, i.e. as long as start() is not invoked.

Parameters
[in]attributesNew attributes to set.
Returns
True on success, false on error (if the thread is already started).

◆ getAttributes()

void ts::Thread::getAttributes ( ThreadAttributes attributes)

Get a copy of the attributes of the thread.

Parameters
[out]attributesAttributes of the thread.

◆ start()

bool ts::Thread::start ( )

Start the thread.

The operating system thread is created and started. The code which is executed in the context of this thread is in the method main().

Returns
True on success, false on error (operating system error or the thread is already started).

◆ waitForTermination()

bool ts::Thread::waitForTermination ( )

Wait for thread termination.

The thread which invokes this method is blocked until the execution of this thread object completes.

Only one waiter thread is allowed. If several threads concurrently invoke waitForTermination() on the same Thread object, only the first one will wait. The method waitForTermination() returns an error to all other threads.

This method is automatically invoked in the destructor. Thus, when a Thread object is declared in a control block and the thread has been started, the end of the control block hangs as long as the thread is not terminated. If the thread has not been started, however, the destructor does not wait (otherwise it would hang for ever).

Important: When a subclass of Thread has non-static members, its destructor shall invoke waitForTermination(). Thus, it prevents its members from being destructed until the thread terminates. If the destructor of the subclass does not invoke waitForTermination() and the Thread object goes out of scope before the termination of the thread, the subclass part of the object is destroyed. Any attempt to access non-static members from the main() method in the context of the thread will give unexpected results. Most of the time, this will result in an error similar to "pure virtual method called". To avoid this:

class MyThread: public ts::Thread
{
public:
virtual ~MyThread()
{
}
...
};

Do not use this method if the thread was created with the delete when terminated flag (ts::ThreadAttributes::setDeleteWhenTerminated).

Returns
True on success, false on error. Errors include operating system errors, the thread is not yet started, the caller thread is this thread (waiting for ourself would result in a dead-lock).

◆ isCurrentThread()

bool ts::Thread::isCurrentThread ( ) const

Check if the caller is running in the context of this thread.

Returns
True if the caller of isCurrentThread() is running in the context of this thread.

◆ main()

virtual void ts::Thread::main ( )
pure virtual

This hook is invoked in the context of the thread.

Concrete thread classes shall implement this pure virtual method. This method is invoked in the context of the created thread when it is started.

◆ Yield()

static void ts::Thread::Yield ( )
static

Yield execution of the current thread.

Execution is passed to another thread, if any is waiting for execution. This should not change the behaviour of correctly-written applications.


The documentation for this class was generated from the following file: