TSDuck Version 3.32-2780 (TSDuck - The MPEG Transport Stream Toolkit)
ts::ThreadLocalObjects Class Reference

Thread local objects. More...

Public Member Functions

void deleteLocalObjects ()
 Delete all local objects in the current thread.
 
ObjectPtr getLocalObject (const UString &name)
 Get the value of a thread local object. More...
 
void setLocalObject (const UString &name, const ObjectPtr &obj)
 Set the value of a thread local object. More...
 

Static Public Member Functions

static ThreadLocalObjectsInstance ()
 Get the instance of the singleton of this class. More...
 

Detailed Description

Thread local objects.

System-agnostic interface to "thread local storage" (Windows) or "thread specific data" (POSIX thread). This class is a singleton which gives access to all local objects of the current thread.

When a thread terminates, all its local objects which were inserted using this singleton are deleted.

The actual time where these deletions occur depends on the thread and the operating system.

  • If the thread is an instance of ts::Thread, the deletions occur when the Thread::main() method returns (see the private method Thread::mainWrapper()).
  • Otherwise, on Linux and macOS (pthread-based), the deletion occurs when the pthread is deleted. It has been noted that it is sometimes too late, depending on what the destructors are doing. For instance, detaching a thread from the Java Virtual Machine (JVM) when a native thread was used in a Java Native Interface (JNI) method is too late. The JVM will wait indefinitely for the native thread to terminate.
  • Otherwise, on Windows, the deletions occur only when TSDuck is used as a DLL because DllMain() is the only known way to execute some code on thread exit. When TSDuck is used as a static library, thread local objects are never deallocated. If you know a solution to this problem, please submit a fix through a pull request.

In other words, ThreadLocalObjects is really safe only on threads which were created by ts::Thread.

Member Function Documentation

◆ Instance()

static ThreadLocalObjects* ts::ThreadLocalObjects::Instance ( )
static

Get the instance of the singleton of this class.

Returns
The instance of the singleton of this class.

◆ setLocalObject()

void ts::ThreadLocalObjects::setLocalObject ( const UString name,
const ObjectPtr obj 
)

Set the value of a thread local object.

Parameters
[in]nameName of the object.
[in]objA smart pointer to the object with that name in the current thread.

◆ getLocalObject()

ObjectPtr ts::ThreadLocalObjects::getLocalObject ( const UString name)

Get the value of a thread local object.

Parameters
[in]nameName of the object.
Returns
A smart pointer to the object with that name in the current thread or the null pointer if there is none.

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