Base Framework
Classes | Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | List of all members
Thread Class Reference

Thread. More...

#include <base/concurrency/Thread.h>

Inherits Runnable.

Classes

class  PerformanceCounters
 
class  Self
 Exception raised by Thread. More...
 
class  ThreadException
 Thread exception. More...
 
class  Times
 
class  UseThreadLocalBuffer
 

Public Types

enum  SchedulingPolicy { INHERITED, FIFO, REALTIME, OTHER }
 
enum  State {
  NOTSTARTED, STARTING, ALIVE, TERMINATED,
  EXIT, EXCEPTION, CANCEL, INTERNAL
}
 
enum  Priority { LOWEST_PRIORITY, NORMAL_PRIORITY, HIGHEST_PRIORITY, HIGHEST_REALTIME_PRIORITY }
 
typedef void * Identifier
 

Public Member Functions

 Thread ()
 
 Thread (Runnable *runnable)
 
ThreadgetParent () const noexcept
 
State getState () const noexcept
 
bool isAlive () const noexcept
 
bool isAncestor () const noexcept
 
bool isChild () const noexcept
 
bool isMainThread () const noexcept
 
unsigned int getThreadId () const noexcept
 
bool isParent () const noexcept
 
bool isSelf () const noexcept
 
bool isTerminated () const noexcept
 
bool join () const
 
void run ()
 
void start ()
 
void terminate () noexcept
 
 ~Thread ()
 
- Public Member Functions inherited from Runnable
 Runnable () noexcept
 
bool isTerminated () noexcept
 
virtual void onChild (Thread *child) noexcept
 
virtual void onTermination () noexcept
 
virtual ~Runnable ()
 

Static Public Member Functions

static int getNamedPriority (Priority priority) noexcept
 
static Identifier getIdentifier () noexcept
 
static void exit () noexcept
 
static ThreadLocalContextgetLocalContext () noexcept
 
static PerformanceCounters getPerformanceCounters () noexcept
 
static ThreadgetThread () noexcept
 
static void nanosleep (unsigned int nanoseconds)
 
static void microsleep (unsigned int microseconds)
 
static void millisleep (unsigned int milliseconds)
 
static void sleep (unsigned int seconds)
 
static void yield () noexcept
 
static bool isStandalone () noexcept
 
static int getPriority ()
 
static Times getTimes () noexcept
 
static unsigned int getThreadSimpleId () noexcept
 
static String getThreadName () noexcept
 
static void setThreadName (const char *name)
 

Static Public Attributes

static constexpr bool SUPPORTS_THREADING = false
 
static constexpr MemorySize THREAD_LOCAL_STORAGE = 4096
 

Protected Member Functions

RunnablegetRunnable () noexcept
 
void onChildTermination (Thread *child)
 

Additional Inherited Members

- Protected Attributes inherited from Runnable
bool terminated = false
 

Detailed Description

Thread.

Thread (a single flow of control).

Example:

class MyActiveObject : public Runnable {
protected:
unsigned int count = 0;
public:
MyThread(unsigned int _count) : count(_count) {
}
void run() {
setThreadName("MyThread");
while (count--) {
}
}
};
class MyApplication : public Application {
public:
void main() {
MyActiveObject myActiveObject(100);
Thread myThread(myActiveObject);
myThread.start(); // start thread
myThread.join(); // wait for thread to complete
}
};
See also
Runnable
Version
1.3
Examples
testsuite/echod.cpp, and testsuite/Thread.cpp.

Member Typedef Documentation

◆ Identifier

typedef void* Thread::Identifier

Thread resource identifier type.

Member Enumeration Documentation

◆ Priority

Enumerator
LOWEST_PRIORITY 

Asks for the lowest priority level.

NORMAL_PRIORITY 

Asks for the normal priority level (which is 0).

HIGHEST_PRIORITY 

Asks for the highest recommended priority level.

HIGHEST_REALTIME_PRIORITY 

Asks for the highest supported priority level.

◆ SchedulingPolicy

Scheduling policy type.

Enumerator
INHERITED 

Scheduling policy is inherited from parent to child thread.

FIFO 

First-in-first-out policy.

REALTIME 

Real time scheduling policy.

OTHER 

Unspecified policy.

◆ State

Thread running state.

Enumerator
NOTSTARTED 

Thrad has been created but not started.

STARTING 

Thread has been started but may not have begun execution.

ALIVE 

Thread is running.

TERMINATED 

Thread has exited normally.

EXIT 

Thread was exited using Thread::exit().

EXCEPTION 

Thread has exited due to uncaught exception.

CANCEL 

Thread was cancelled.

INTERNAL 

Thread was exited due to internal exception - please forgive me.

Constructor & Destructor Documentation

◆ Thread() [1/2]

Thread::Thread ( )

Initializes thread object. The thread is suspended until it is explicitly started.

◆ Thread() [2/2]

Thread::Thread ( Runnable runnable)

Initializes thread object. The thread is suspended until it is explicitly started.

Parameters
runnableThe desired object to be run when the thread is started.

◆ ~Thread()

Thread::~Thread ( )

Destroys the thread object. The calling thread blocks until the thread has completed. It is also valid to destroy a thread which has never been started. The thread will dead-lock if it tries to destroy itself. The resources associated with the thread context may linger for some time until the operating system desides its time to release them.

Member Function Documentation

◆ exit()

static void Thread::exit ( )
staticnoexcept

Exits the executing thread. May result in resource leaks if objects have been created on the heap. It is not recommended that you use this method.

◆ getIdentifier()

static Identifier Thread::getIdentifier ( )
staticnoexcept

Returns the identifier of the executing thread.

◆ getLocalContext()

static ThreadLocalContext* Thread::getLocalContext ( )
staticnoexcept

Returns the thread object associated with the executing thread.

◆ getNamedPriority()

static int Thread::getNamedPriority ( Priority  priority)
staticnoexcept

Returns the priority level corresponding to the named priority.

Parameters
priorityThe named priority.

◆ getParent()

Thread* Thread::getParent ( ) const
inlinenoexcept

Returns the thread that created this thread. Returns 0 for the main thread.

◆ getPerformanceCounters()

static PerformanceCounters Thread::getPerformanceCounters ( )
staticnoexcept

Returns the thread performance counters. Requires Profiler to be enabled.

◆ getPriority()

static int Thread::getPriority ( )
static

Returns the priority level of the executing thread.

◆ getRunnable()

Runnable* Thread::getRunnable ( )
inlineprotectednoexcept

Returns the active object.

◆ getState()

State Thread::getState ( ) const
inlinenoexcept

Returns the execution state of the thread.

◆ getThread()

static Thread* Thread::getThread ( )
staticnoexcept

Returns the thread object associated with the executing thread.

Examples
testsuite/echod.cpp.

◆ getThreadId()

unsigned int Thread::getThreadId ( ) const
inlinenoexcept

Returns the simple ID assigned to the thread.

◆ getThreadName()

static String Thread::getThreadName ( )
staticnoexcept

Returns the thread name is available.

◆ getThreadSimpleId()

static unsigned int Thread::getThreadSimpleId ( )
staticnoexcept

Returns the simple ID assigned to the thread.

◆ getTimes()

static Times Thread::getTimes ( )
staticnoexcept

Returns the current processing times (both user and system times).

◆ isAlive()

bool Thread::isAlive ( ) const
noexcept

Returns true if the thread is alive and kicking.

◆ isAncestor()

bool Thread::isAncestor ( ) const
noexcept

Returns true if the executing thread is an ancestor of this thread.

◆ isChild()

bool Thread::isChild ( ) const
noexcept

Returns true if the executing thread is a child of this thread.

◆ isMainThread()

bool Thread::isMainThread ( ) const
inlinenoexcept

Returns true if the thread is the main thread.

◆ isParent()

bool Thread::isParent ( ) const
noexcept

Returns true if the executing thread is the parent of this thread.

◆ isSelf()

bool Thread::isSelf ( ) const
noexcept

Returns true if the thread is the executing thread.

◆ isStandalone()

static bool Thread::isStandalone ( )
staticnoexcept

Returns true if the executing thread is the only thread within the process. This method is experimental!

◆ isTerminated()

bool Thread::isTerminated ( ) const
inlinenoexcept

Returns true if the thread has been asked to terminate.

Examples
testsuite/echod.cpp.

◆ join()

bool Thread::join ( ) const

The calling thread waits for the thread complete. Several threads are allowed to be waiting for the same thread to complete. A thread will block indefinitely if it tries to join itself. The resources associated with the thread context may linger for some time until the operating system desides its time to release them. It is, however, legal to destroy the thread object after join() has returned.

Returns
The method returns false if the thread has not been started and otherwise true.
Examples
testsuite/echod.cpp.

◆ microsleep()

static void Thread::microsleep ( unsigned int  microseconds)
static

Makes the executing thread sleep for at least the specified time.

Parameters
microsecondsThe desired time in microseconds to make the thread sleep. The value must be within the domain from 0 to 999999999.
Examples
testsuite/ping.cpp.

◆ millisleep()

static void Thread::millisleep ( unsigned int  milliseconds)
static

Makes the executing thread sleep for at least the specified time.

Parameters
millisecondsThe desired time in milliseconds to make the thread sleep. The value must be within the domain from 0 to 999999999.

◆ nanosleep()

static void Thread::nanosleep ( unsigned int  nanoseconds)
static

Makes the executing thread sleep for at least the specified time.

Parameters
nanosecondsThe desired time in nanoseconds to make the thread sleep. The value must be within the domain from 0 to 999999999.

◆ onChildTermination()

void Thread::onChildTermination ( Thread child)
protected

Invoked by a child thread of this thread upon termination. Only threads that have been started result in a notification on termination.

Parameters
childThe child thread.

◆ run()

void Thread::run ( )
virtual

Entry point for the thread.

Implements Runnable.

◆ setThreadName()

static void Thread::setThreadName ( const char *  name)
static

Sets thread name.

Examples
testsuite/Thread.cpp.

◆ sleep()

static void Thread::sleep ( unsigned int  seconds)
static

Makes the executing thread sleep for at least the specified time.

Parameters
secondsThe desired time in seconds to make the thread sleep. The value must be within the domain from 0 to 999999.

◆ start()

void Thread::start ( )

Starts the thread. The underlying context of execution is allocated here and not in the thread constructor.

Examples
testsuite/echod.cpp.

◆ terminate()

void Thread::terminate ( )
noexcept

Asks the thread to terminate as soon as possible. This does not block the executing thread.

Examples
testsuite/echod.cpp.

◆ yield()

static void Thread::yield ( )
staticnoexcept

Relinquishes the time slot of currently executing thread voluntarily without blocking. Notifies the scheduler that the current thread is willing to release its time slice to other threads of the same or higher priority.

Examples
testsuite/Thread.cpp.

Member Data Documentation

◆ THREAD_LOCAL_STORAGE

constexpr MemorySize Thread::THREAD_LOCAL_STORAGE = 4096
staticconstexpr

Specifies the size of the thread local storage.

Runnable
Base class of active objects.
Definition: Runnable.h:32
Thread
Thread.
Definition: Thread.h:77
Thread::run
void run()
Application
Application.
Definition: Application.h:53
Thread::setThreadName
static void setThreadName(const char *name)