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

The class DoubleCheckLock implements the double check locking design pattern. More...

Classes

class  Reader
 Guard class for the reader thread. More...
 
class  Writer
 Guard class for writer threads. More...
 

Public Member Functions

 DoubleCheckLock ()
 Default constructor.
 
bool changed () const
 Check if the shared data have been marked as "changed". More...
 

Detailed Description

The class DoubleCheckLock implements the double check locking design pattern.

The double check locking design pattern is used to protect shared data with the following characteristics:

  • One single reader thread.
  • The data are frequently read.
  • One (or more) writer thread.
  • The data are much less frequently written that read.

The idea is that the reader thread uses its own private copy of the protected data and uses it without locking. At predefined points, the reader checks if the data have changed. When this is the case, a new copy is fetched under the protection of a mutex.

The writer threads always update the data under the protection of the mutex.

This method is slightly more complicated than using a mutex for each shared data access sequence but it significantly reduces the synchronization overhead, provided that the above conditions are met.

The following is a usage template.

Shared data

SomeDataType data; // the data to protect
ts::DoubleCheckLock lock; // associated lock

Writer threads (not so often)

{
// Acquire lock as writer
// Modify data
data = ....
}

Reader thread

SomeDataType copy; // Private copy of data (or part of it)
if (lock.changed()) {
// Acquire lock as reader
// Get a copy of data
copy = data;
}
// Then, use "copy" (private to reader thread) instead of "data"

Member Function Documentation

◆ changed()

bool ts::DoubleCheckLock::changed ( ) const
inline

Check if the shared data have been marked as "changed".

This method is invoked in the reader thread to check if the data have been modified by a writer thread.

Returns
True if a writer thread has modified the data since the last time a DoubleCheckLock::Reader has been used, false otherwise.

The documentation for this class was generated from the following file:
ts::DoubleCheckLock
The class DoubleCheckLock implements the double check locking design pattern.
Definition: tsDoubleCheckLock.h:102
ts::DoubleCheckLock::Reader
Guard class for the reader thread.
Definition: tsDoubleCheckLock.h:146
ts::DoubleCheckLock::Writer
Guard class for writer threads.
Definition: tsDoubleCheckLock.h:126
ts::DoubleCheckLock::changed
bool changed() const
Check if the shared data have been marked as "changed".
Definition: tsDoubleCheckLock.h:121