TSDuck Version 3.20-1548 (TSDuck - The MPEG Transport Stream Toolkit)
ts::Mutex Class Reference

A mutex implementation which is compatible with the ts::Thread class. More...

Inheritance diagram for ts::Mutex:
Collaboration diagram for ts::Mutex:

Classes

class  MutexError
 Fatal low-level mutex error. More...
 

Public Member Functions

 Mutex ()
 Default constructor. More...
 
virtual ~Mutex () override
 Destructor.
 
virtual bool acquire (MilliSecond timeout=Infinite) override
 Acquire the mutex with a timeout. More...
 
virtual bool release () override
 Release the mutex. More...
 

Detailed Description

A mutex implementation which is compatible with the ts::Thread class.

The concrete class ts::Mutex is a mutex implementation which is compatible with the ts::Thread class. This means that concurrent instances of ts::Thread can synchronize mutual exclusion on shared resources using instances of ts::Mutex.

This mutex implementation is recursive, i.e. nested pairs of acquire() / release() are allowed on the instances. There must be exactly one release() for each nested invocation of acquire().

The implementation of this class is operating system dependent, just like ts::Thread.

Constructor & Destructor Documentation

◆ Mutex()

ts::Mutex::Mutex ( )

Default constructor.

Exceptions
MutexErrorIn case of operating system error, when the underlying system objects could not be created.

Member Function Documentation

◆ acquire()

virtual bool ts::Mutex::acquire ( MilliSecond  timeout = Infinite)
overridevirtual

Acquire the mutex with a timeout.

If the mutex is already acquired by another instance of ts::Thread, acquire() hangs until the mutex is released or the timeout expires, whichever comes first.

If the mutex is already acquired by the current ts::Thread, acquire() completes immediately. For each successful call to acquire() in the current ts::Thread, there must be exactly one call to release().

If a ts::Thread which holds a mutex terminates before it has released the mutex, this mutex is automatically released.

Parameters
[in]timeoutMaximum number of milliseconds to wait for the mutex.
Returns
True on success and false on error or when the timeout expires.

Implements ts::MutexInterface.

◆ release()

virtual bool ts::Mutex::release ( )
overridevirtual

Release the mutex.

For each successful call to acquire(), there must be exactly one call to release().

Important: If a mutex is destroyed while it is still acquired, the results are unpredictible. Experience has shown than destroying a mutex while it is acquired is harmless on Windows, macOS and Linux with glibc. However, on Linux with musl libc (Alpine Linux for instance), failing to call release() as many times as acquire() leads to random memory corruptions and crashes.

For this reason, it is recommended to never use acquire() and release() directly and use the Guard class instead.

Returns
true on success and false on error.

Implements ts::MutexInterface.


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