TSDuck v3.38-3699
MPEG Transport Stream Toolkit
Loading...
Searching...
No Matches
ts::Thread Class Referenceabstract

Base class for threads. More...

#include <tsThread.h>

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).
 
 Thread (const ThreadAttributes &attributes)
 Constructor from specified attributes.
 
virtual ~Thread ()
 Destructor.
 
void getAttributes (ThreadAttributes &attributes)
 Get a copy of the attributes of the thread.
 
UString getTypeName () const
 Get an implementation-specific name of the object class.
 
bool isCurrentThread () const
 Check if the caller is running in the context of this thread.
 
virtual void main ()=0
 This hook is invoked in the context of the thread.
 
bool setAttributes (const ThreadAttributes &attributes)
 Set new attributes to the thread.
 
bool start ()
 Start the thread.
 
bool waitForTermination ()
 Wait for thread termination.
 

Static Public Member Functions

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

Protected Member Functions

void setTypeName (const UString &name=UString())
 Set the type name.
 

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.

The C++11 standard class std::thread is not used because it does not provide any way to set specific thread properties such as stack size. std::thread provides a method named native_handle() which may be used to call non-portable features. However, native_handle() becomes accessible only after the thread is started, too late to set properties such as stack size. This makes std::thread unusable outside basic simple tasks without specific requirements.

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));
Set of attributes for a thread object (ts::Thread).
Definition tsThreadAttributes.h:23
Base class for threads.
Definition tsThread.h:39
Parameters
[in]attributesThe set of attributes.

◆ ~Thread()

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

Destructor.

All subclasses should wait for termination before exiting their destructor. If this is not done, the thread could continue its parallel execution while the member fields are destructed, which is invalid. As a fool-proof check, the parent class Thread checks that the thread is actually terminated in its own destructor and report a fatal error message on standard error if this is not the case.

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.

◆ getTypeName()

UString ts::Thread::getTypeName ( ) const

Get an implementation-specific name of the object class.

Returns
An implementation-specific name of the object class. The result may be not portable. The returned value may be empty before start().

◆ 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()
{
waitForTermination();
}
...
};

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.

◆ setTypeName()

void ts::Thread::setTypeName ( const UString name = UString())
protected

Set the type name.

Parameters
[in]nameThe type name to set. If empty, the subclass type name is used.

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