libcamera
v0.0.0
Supporting cameras in Linux since 2019
|
A thread of execution. More...
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 |
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.
|
static |
|
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.
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.
|
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.
void libcamera::Thread::exit | ( | int | code = 0 | ) |
bool libcamera::Thread::isRunning | ( | ) |
|
protectedvirtual |
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.
void libcamera::Thread::setEventDispatcher | ( | std::unique_ptr< EventDispatcher > | dispatcher | ) |
Set the event dispatcher.
[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.
bool libcamera::Thread::wait | ( | utils::duration | duration = utils::duration::max() | ) |
Wait for the thread to finish.
[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.