libcamera::Thread Class Reference

A thread of execution. More...

Inheritance diagram for libcamera::Thread:

## Public Member Functions

| Thread ()
| Create a thread.
void | start ()
| Start the thread.
void | exit (int code=0)
| Stop the thread's event loop. More...
bool | wait (utils::duration duration=utils::duration::max())
| Wait for the thread to finish. More...
bool | isRunning ()
| Check if the thread is running. More...
EventDispatcher * | eventDispatcher ()
| Retrieve the event dispatcher. More...
void | setEventDispatcher (std::unique_ptr< EventDispatcher > dispatcher)
| Set the event dispatcher. More...
void | dispatchMessages ()
| Dispatch all posted messages for this thread.

## Static Public Member Functions

static Thread * | current ()
| Retrieve the Thread instance for the current thread. More...
static pid_t | currentId ()
| Retrieve the ID of the current thread. More...

## Public Attributes

Signal< Thread * > | finished
| Signal the end of thread execution.

## Protected Member Functions

int | exec ()
| Enter the event loop. More...
virtual void | run ()
| Main method of the thread. More...

## Friends

class | Object
class | ThreadData
class | ThreadMain

## Detailed Description

A thread of execution.

The Thread class is a wrapper around std::thread that handles integration with the Object, Signal and EventDispatcher classes.

Thread instances by default run an event loop until the exit() method is called. A custom event dispatcher may be installed with setEventDispatcher(), otherwise a poll-based event dispatcher is used. This behaviour can be overriden by overloading the run() method.

Thread Safety:
  This class is thread-safe.

## Member Function Documentation

## current()

Thread * libcamera::Thread::current | ( |  | ) |

static

Retrieve the Thread instance for the current thread.

Returns
  The Thread instance for the current thread

## currentId()

pid_t libcamera::Thread::currentId | ( |  | ) |

static

Retrieve the ID of the current thread.

The thread ID corresponds to the Linux thread ID (TID) as returned by the gettid system call.

Returns
  The ID of the current thread

## eventDispatcher()

EventDispatcher * libcamera::Thread::eventDispatcher | ( |  | ) |

Retrieve the event dispatcher.

This method retrieves the event dispatcher set with setEventDispatcher(). If no dispatcher has been set, a default poll-based implementation is created and returned, and no custom event dispatcher may be installed anymore.

The returned event dispatcher is valid until the thread is destroyed.

Returns
  Pointer to the event dispatcher

## exec()

int libcamera::Thread::exec | ( |  | ) |

protected

Enter the event loop.

This method enter an event loop based on the event dispatcher instance for the thread, and blocks until the exit() method is called. It is meant to be called within the thread from the run() method and shall not be called outside of the thread.

Returns
  The exit code passed to the exit() method

## exit()

void libcamera::Thread::exit | ( | int | code = `0` | ) |

Stop the thread's event loop.

Parameters

  [in] | code | The exit code

This method interrupts the event loop started by the exec() method, causing exec() to return code.

Calling exit() on a thread that reimplements the run() method and doesn't call exec() will likely have no effect.

## isRunning()

bool libcamera::Thread::isRunning | ( |  | ) |

Check if the thread is running.

A Thread instance is considered as running once the underlying thread has started. This method guarantees that it returns true after the start() method returns, and false after the wait() method returns.

Returns
  True if the thread is running, false otherwise

## run()

void libcamera::Thread::run | ( |  | ) |

protected virtual

Main method of the thread.

When the thread is started with start(), it calls this method in the context of the new thread. The run() method can be overloaded to perform custom work. When this method returns the thread execution is stopped, and the finished signal is emitted.

The base implementation just calls exec().

Reimplemented in libcamera::ThreadMain.

## setEventDispatcher()

void libcamera::Thread::setEventDispatcher | ( | std::unique_ptr< EventDispatcher > | dispatcher | ) |

Set the event dispatcher.

Parameters

  [in] | dispatcher | Pointer to the event dispatcher

Threads that run an event loop require an event dispatcher to integrate event notification and timers with the loop. Users that want to provide their own event dispatcher shall call this method once and only once before the thread is started with start(). If no event dispatcher is provided, a default poll-based implementation will be used.

The Thread takes ownership of the event dispatcher and will delete it when the thread is destroyed.

## wait()

bool libcamera::Thread::wait | ( | utils::duration | duration = `utils::duration::max()` | ) |

Wait for the thread to finish.

Parameters

  [in] | duration | Maximum wait duration

This function waits until the thread finishes or the duration has elapsed, whichever happens first. If duration is equal to utils::duration::max(), the wait never times out. If the thread is not running the function returns immediately.

Returns
  True if the thread has finished, or false if the wait timed out

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

- include/libcamera/internal/thread.h
- src/libcamera/thread.cpp