thread


A thread is a program that is executing. A thread can access almost all of its own attributes, but can access only a small subset of other threads' attributes. Therefore, thread objects are modeled using two classes: os_this_thread , which represents the current thread, and os_thread , which represents other threads.

To create a new thread, construct an os_thread with the address of a function and a pointer to an optional argument. This creates a thread that executes the specified function with the supplied pointer as its argument. The function must be able to take a void* argument and return a void* .

Once constructed, an os_thread object contains functions that allow you to suspend, resume, terminate, and change the priority of the thread object. The new thread executes concurrently with other threads in the process and terminates when it reaches the end of the specified function.

A thread is associated with a priority defined by the enumeration defined below. This enumerator is scoped in the class os_thread_priority . A thread priority is defined by a level_t to allow portable prioritization across most major platforms.

Conversions allow os_thread and os_thread_t to be used interchangeably.

Library

Thread<ToolKit>

Declaration

#include <ospace/thread/thread.h>

class os_thread

Enums

enum os_thread_priority::level_t
  {
  highest = 0,
  above_normal,
  normal,
  below_normal,
  lowest
  };
  

Typedefs

typedef void* (*os_thread_func_t)( void* arg )
// Pointer to a function that takes a void* argument and returns a void*.

Interface

Constructor
os_thread( os_thread_func_t func , void* arg , size_t stack_size , bool detached )
Constructs a new thread. By default, when the thread is constructed, immediately executes the function pointed to by func with the single argument arg (default 0 ). The default value for detached (default false ) causes the thread to run nondetached. The default value for stack_size (default 0 ) causes a default stack size allocation for the thread. If detached is false , the thread termination can be detected using os_this_thread::wait_for_thread() or os_this_thread::wait_for_any_thread().

Throws: os_thread_toolkit_error

Constructor
template< class Runnable >
os_thread( Runnable* runnable , size_t stack_size , bool detached )
Constructs a new thread. When the thread is constructed, immediately executes the void* run() method of runnable . The default value for detached (default false ) causes the thread to run nondetached. The default value for stack_size (default 0 ) causes a default stack size allocation for the thread. If detached is false , the thread termination can be detected using os_this_thread::wait_for_thread() or os_this_thread::wait_for_any_thread(). Note that the compiler must be able to support member templates and OS_ENABLE_MEMBER_TEMPLATE_FEATURES must be defined to use this member function.

Throws: os_thread_toolkit_error

Constructor
os_thread( const os_thread& thread )
Constructs a thread to represent the same thread as thread .
Constructor
os_thread( os_thread_t tid )
Constructs a thread to represent the thread whose identifier is tid .
<
bool operator<( const os_thread& thread ) const
Returns true if the thread ID is less than thread 's.
=
os_thread& operator=( os_thread_t tid )
Associates the thread with the thread whose ID is tid .
==
bool operator==( const os_thread& thread ) const
Returns true if the thread ID is equal to thread 's.
create_thread
/* static */ os_thread_t create_thread( os_thread_func_t func , void* arg , size_t stack_size , bool detached )
Constructs a new thread and returns its handle. The arguments are the same as those described in the os_thread constructor.

Throws: os_thread_toolkit_error

create_thread
template< class Runnable >
/* static */ os_thread_t create_thread( Runnable* runnable , void* arg , size_t stack_size , bool detached )
Constructs a new thread and returns its handle. The arguments are the same as those described in the os_thread constructor.

Throws: os_thread_toolkit_error

exists
bool exists() const
Returns true if the thread represents a thread that has not terminated.
(os_thread_t)
operator os_thread_t() const
Returns the thread ID.
print
void print( ostream& stream ) const
Prints to stream .
priority
void priority( os_thread_priority::level_t level )
Changes thread priority to level .

Throws: os_thread_toolkit_error

priority
os_thread_priority::level_t priority() const
Returns the thread priority level.

Throws: os_thread_toolkit_error

terminate
void terminate( void* status )
Terminates the thread with exit code status . Not available with Solaris native threads.

Throws: os_thread_toolkit_error

tid
os_thread_t tid() const
Returns the thread ID.

Non-Member Functions

<<
ostream& operator<<( ostream& stream , const os_thread& thread )
Prints thread to stream .

Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.