Kea  1.5.0
isc::util::thread::Thread Class Reference

A separate thread. More...

#include <thread.h>

+ Inheritance diagram for isc::util::thread::Thread:

Classes

class  Impl
 
class  UncaughtException
 There's an uncaught exception in a thread. More...
 

Public Member Functions

 Thread (const boost::function< void()> &main)
 Create and start a thread. More...
 
 ~Thread ()
 Destructor. More...
 
void wait ()
 Wait for the thread to terminate. More...
 

Detailed Description

A separate thread.

A thread of execution. When created, starts running in the background. You can wait for it then or just forget it ever existed and leave it live peacefully.

The interface is minimalist for now. We may need to extend it later.

Note
While the objects of this class represent another thread, they are not thread-safe. You're not supposed to call wait() on the same object from multiple threads or so. They are reentrant (you can wait for different threads from different threads).

Definition at line 36 of file thread.h.

Constructor & Destructor Documentation

◆ Thread()

isc::util::thread::Thread::Thread ( const boost::function< void()> &  main)

Create and start a thread.

Create a new thread and run body inside it.

If you need to pass parameters to body, or return some result, you may just want to use boost::bind or alike to store them within the body functor.

Note
The main functor will be copied internally. You need to consider this when returning the result.

The body should terminate by exiting the function. If it throws, it is considered an error. You should generally catch any exceptions form within there and handle them somehow.

Parameters
mainThe code to run inside the thread.
Exceptions
std::bad_allocif allocation of the new thread or other resources fails.
isc::InvalidOperationfor other errors (should not happen).

Definition at line 125 of file thread.cc.

References isc::ha::impl, isc_throw, main(), and isc::util::thread::Thread::Impl::run().

+ Here is the call graph for this function:

◆ ~Thread()

isc::util::thread::Thread::~Thread ( )

Destructor.

It is completely legitimate to destroy the thread without calling wait() before. In such case, the thread will just live on until it terminates. However, if the thread dies due to exception, for example, it's up to you to detect that, no error is reported from this class.

Exceptions
isc::InvalidOperationin the rare case of OS reporting a problem. This should not happen unless you messed up with the raw thread by the low-level API.

Definition at line 144 of file thread.cc.

References isc::util::thread::Thread::Impl::done(), and isc::util::thread::Thread::Impl::tid_.

+ Here is the call graph for this function:

Member Function Documentation

◆ wait()

void isc::util::thread::Thread::wait ( )

Wait for the thread to terminate.

Waits until the thread terminates. Must be called at most once.

Exceptions
isc::InvalidOperationif the OS API returns error. This usually mean a programmer error (like two threads trying to wait on each other).
isc::InvalidOperationcalling wait a second time.
UncaughtExceptionif the thread terminated by throwing an exception instead of just returning from the function.

Definition at line 156 of file thread.cc.

References isc::util::thread::Thread::Impl::done(), isc::util::thread::Thread::Impl::exception_, isc::util::thread::Thread::Impl::exception_text_, isc_throw, and isc::util::thread::Thread::Impl::tid_.

+ Here is the call graph for this function:

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