Thread.
More...
#include <base/concurrency/Thread.h>
Inherits Runnable.
|
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 |
|
|
static constexpr bool | SUPPORTS_THREADING = false |
|
static constexpr MemorySize | THREAD_LOCAL_STORAGE = 4096 |
|
Thread.
Thread (a single flow of control).
Example:
class MyActiveObject :
public Runnable {
protected:
unsigned int count = 0;
public:
MyThread(unsigned int _count) : count(_count) {
}
while (count--) {
}
}
};
public:
void main() {
MyActiveObject myActiveObject(100);
Thread myThread(myActiveObject);
myThread.start();
myThread.join();
}
};
- See also
- Runnable
- Version
- 1.3
- Examples
- testsuite/echod.cpp, and testsuite/Thread.cpp.
◆ Identifier
Thread resource identifier type.
◆ 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.
|
◆ Thread() [1/2]
Initializes thread object. The thread is suspended until it is explicitly started.
◆ Thread() [2/2]
Initializes thread object. The thread is suspended until it is explicitly started.
- Parameters
-
runnable | The desired object to be run when the thread is started. |
◆ ~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.
◆ 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()
Returns the identifier of the executing thread.
◆ getLocalContext()
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
-
priority | The named priority. |
◆ getParent()
Thread* Thread::getParent |
( |
| ) |
const |
|
inlinenoexcept |
Returns the thread that created this thread. Returns 0 for the main thread.
◆ getPerformanceCounters()
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()
Returns the active object.
◆ getState()
State Thread::getState |
( |
| ) |
const |
|
inlinenoexcept |
Returns the execution state of the thread.
◆ getThread()
static Thread* Thread::getThread |
( |
| ) |
|
|
staticnoexcept |
◆ 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 |
◆ 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
-
microseconds | The 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
-
milliseconds | The 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
-
nanoseconds | The 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
-
◆ run()
Entry point for the thread.
Implements Runnable.
◆ setThreadName()
static void Thread::setThreadName |
( |
const char * |
name | ) |
|
|
static |
◆ sleep()
static void Thread::sleep |
( |
unsigned int |
seconds | ) |
|
|
static |
Makes the executing thread sleep for at least the specified time.
- Parameters
-
seconds | The desired time in seconds to make the thread sleep. The value must be within the domain from 0 to 999999. |
◆ 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.
◆ THREAD_LOCAL_STORAGE
constexpr MemorySize Thread::THREAD_LOCAL_STORAGE = 4096 |
|
staticconstexpr |
Specifies the size of the thread local storage.