TSDuck v3.40-4033
MPEG Transport Stream Toolkit
|
Base class for threads. More...
#include <tsThread.h>
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. | |
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 | |
virtual void | main ()=0 |
This hook is invoked in the context of the thread. | |
void | setTypeName (const UString &name=UString()) |
Set the type name. | |
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.
ts::Thread::Thread | ( | ) |
Default constructor (all attributes have their default values).
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:
[in] | attributes | The set of attributes. |
|
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.
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.
[in] | attributes | New attributes to set. |
void ts::Thread::getAttributes | ( | ThreadAttributes & | attributes | ) |
Get a copy of the attributes of the thread.
[out] | attributes | Attributes of the thread. |
UString ts::Thread::getTypeName | ( | ) | const |
Get an implementation-specific name of the object class.
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().
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:
Do not use this method if the thread was created with the delete when terminated flag (ts::ThreadAttributes::setDeleteWhenTerminated).
bool ts::Thread::isCurrentThread | ( | ) | const |
Check if the caller is running in the context of this thread.
|
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.
|
protectedpure 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.
Set the type name.
[in] | name | The type name to set. If empty, the subclass type name is used. |