Valkka  1.6.1
OpenSource Video Management
Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Private Member Functions | List of all members
Thread Class Referenceabstract

A class for multithreading with a signaling system. More...

#include <thread.h>

Inheritance diagram for Thread:
Inheritance graph
[legend]

Public Member Functions

 Thread (const char *name)
 Default constructor. More...
 
 ~Thread ()
 Destructor:not virtual. More...
 
virtual void run ()=0
 Main execution loop is defined here.
 
virtual void preRun ()=0
 Called before entering the main execution loop, but after creating the thread.
 
virtual void postRun ()=0
 Called after the main execution loop exits, but before joining the thread.
 
virtual void preJoin ()
 Called before the thread is joined.
 
virtual void postJoin ()
 Called after the thread has been joined.
 
virtual void sendSignal (SignalContext signal_ctx)
 Send a signal to the thread.
 
virtual void sendSignalAndWait (SignalContext signal_ctx)
 Send a signal to the thread and wait for all signals to be executed.
 
void setAffinity (int i)
 API method for setting the thread affinity. More...
 
void startCall ()
 API method: starts the thread.
 
virtual void stopCall ()
 API method: stops the thread. More...
 
virtual void requestStopCall ()
 API method: stops the thread. More...
 
virtual void waitStopCall ()
 API method: waits until the thread is joined. More...
 
virtual void waitReady ()
 Wait until thread has processed all its signals.
 

Protected Member Functions

void mainRun ()
 Does the preRun, run, postRun sequence.
 
void closeThread ()
 Sends exit signal to the thread, calls join. This method blocks until thread has exited. Set Thread::has_thread to false.
 

Static Protected Member Functions

static void * mainRun_ (void *p)
 

Protected Attributes

std::string name
 Name of the thread.
 
bool has_thread
 true if thread has been started
 
bool stop_requested
 
bool thread_joined
 
std::mutex start_mutex
 Mutex protecting start_condition.
 
std::condition_variable start_condition
 Notified when the thread has been started.
 
std::mutex mutex
 Mutex protecting the condition variable and signal queue.
 
std::condition_variable condition
 Condition variable for the signal queue (triggered when all signals processed). Not necessarily used by all subclasses.
 
std::mutex loop_mutex
 Protects thread's main execution loop (if necessary)
 
std::deque< SignalContextsignal_fifo
 Signal queue (fifo). Redefine in child classes.
 
bool loop
 Use this boolean to control if the main loop in Thread:run should exit.
 
int core_id
 
pthread_attr_t thread_attr
 Thread attributes, pthread_* way.
 
cpu_set_t cpuset
 
pthread_t internal_thread
 

Private Member Functions

 Thread (const Thread &)
 Void copy-constructor: this class is non-copyable. More...
 
void operator= (const Thread &)
 Void copy-constructor: this class is non-copyable.
 

Detailed Description

A class for multithreading with a signaling system.

Thread class has a simple system for receiving signals. Signals are placed into a mutex protected fifo queue. The internal struct SignalContext defines data structures passed by the signals. Subclasses typically implement their own SingalContext

Constructor & Destructor Documentation

◆ Thread() [1/2]

Thread::Thread ( const char *  name)

Default constructor.

Parameters
name- name of the thread
core_id- bind to a specific processor. Default=-1, i.e. no processor affinity

◆ ~Thread()

Thread::~Thread ( )

Destructor:not virtual.

Each subclass needs to invoke it's own Thread::stopCall() method

◆ Thread() [2/2]

Thread::Thread ( const Thread )
private

Void copy-constructor: this class is non-copyable.

We have a mutex member in this class. Those are non-copyable. Other possibility would be to manage a pointer to the mutex. The "pointerization" can be done at some other level as well (say, using pointers of this object)

Copying threads is not a good idea either

Member Function Documentation

◆ requestStopCall()

void Thread::requestStopCall ( )
virtual

API method: stops the thread.

Like Thread::stopCall() but does not block. Waiting for the thread to join is done in Thread::waitStoppedCall()

Reimplemented in LiveThread, OpenGLThread, ValkkaFSWriterThread, FileCacheThread, FileThread, USBDeviceThread, FDWriteThread, DecoderThread, and ValkkaFSReaderThread.

◆ setAffinity()

void Thread::setAffinity ( int  i)

API method for setting the thread affinity.

Use before starting the thread

◆ stopCall()

void Thread::stopCall ( )
virtual

API method: stops the thread.

If Thread::has_thread is true, sends exit signal to the thread and calls Thread::closeThread Waits until the thread is joined

Reimplemented in TestThread.

Here is the call graph for this function:

◆ waitStopCall()

void Thread::waitStopCall ( )
virtual

API method: waits until the thread is joined.

Use with Thread::requestStopCall

Here is the call graph for this function:

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