BDE 4.14.0 Production release
|
Typedefs | |
typedef void *(* | bslmt_ThreadFunction) (void *) |
typedef void(* | bslmt_KeyDestructorFunction) (void *) |
Provide platform-independent utilities related to threading.
This component defines a utility struct
, bslmt::ThreadUtil
, that serves as a name space for a suite of pure functions to create threads, join them (make one thread block and wait for another thread to exit), manipulate thread handles, manipulate the current thread, and (on some platforms) access thread-local storage.
Clients call bslmt::ThreadUtil::create()
to create threads. Threads may be started using a "C" linkage function pointer (of a type defined by bslmt::ThreadUtil::ThreadFunction
) and a void
pointer to userData
to be passed to the function; or an "invokable" object of parameterized type (any copy-constructible type on which operator()
may be invoked). The invoked function becomes the main driver for the new thread; when it returns, the thread terminates.
A thread is identified by an object of the opaque type bslmt::ThreadUtil::Handle
. A handle of this type is returned when a thread is created (using bslmt::ThreadUtil::create
). A client can also retrieve a Handle
for the "current" thread via the self
method:
Several thread manipulation functions in bslmt::ThreadUtil
take a thread handle, or pointer to a thread handle, as an argument. To facilitate compatibility with existing systems and allow for non-portable operations, clients also have access to the bslmt::ThreadUtil::NativeHandle
type, which exposes the underlying, platform-specific thread identifier type:
Note that the returned native handle may not be a globally unique identifier for the thread, and, e.g., should not be converted to an integer identifier, or used as a key in a map.
bslmt::ThreadUtil
allows clients to configure the priority of newly created threads by setting the inheritSchedule
, schedulingPolicy
, and schedulingPriority
of a thread attributes object supplied to the create
method. The range of legal values for schedulingPriority
depends on both the platform and the value of schedulingPolicy
, and can be obtained from the getMinSchedulingPriority
and getMaxSchedulingPriority
methods. Both schedulingPolicy
and schedulingPriority
are ignored unless inheritSchedule
is false
(the default value is true
). Note that not only is effective setting of thread priorities workable on only some combinations of platforms and user privileges, but setting the thread policy and priority appropriately for one platform may cause thread creation to fail on another platform. Also note that an unset thread priority may be interpreted as being outside the valid range defined by [ getMinSchedulingPriority(policy), getMaxSchedulingPriority(policy) ]
.
bsls::SystemClockType
supplies the enumeration indicating the system clock on which timeouts supplied to other methods should be based. If the clock type indicated at construction is bsls::SystemClockType::e_REALTIME
, the absTime
argument passed to the timedWait
method of the various synchronization primitives offered in bslmt
should be expressed as an absolute offset since 00:00:00 UTC, January 1, 1970 (which matches the epoch used in bsls::SystemTime::now(bsls::SystemClockType::e_REALTIME)
. If the clock type indicated at construction is bsls::SystemClockType::e_MONOTONIC
, the absTime
argument passed to the timedWait
method of the various synchronization primitives offered in bslmt
should be expressed as an absolute offset since the epoch of this clock (which matches the epoch used in bsls::SystemTime::now(bsls::SystemClockType::e_MONOTONIC)
.
This section illustrates the intended use of this component.
In this example, we create a thread using the default attribute settings. Upon creation, the thread executes the user-supplied C-linkage function myThreadFunction
that counts 5 seconds before terminating:
First, we create a function that will run in the spawned thread:
Now, we show how to create and join the thread.
After creating the thread, the main
routine joins the thread, which, in effect, causes main
to wait for execution of myThreadFunction
to complete, and guarantees that the output from main
will follow the last output from the user-supplied function:
Finally, the output of this program is:
In this example, we will choose to override the default thread attribute values.
The attributes of a thread can be specified explicitly by supplying a bslmt::ThreadAttributes
object to the create
method. For instance, we could specify a smaller stack size for a thread to conserve system resources if we know that we will require not require the platform's default stack size.
First, we define our thread function, noting that it doesn't need much stack space:
Finally, we show how to create a detached thread running the function just created with a small stack size:
In this example we demonstrate creating 3 threads with different priorities. We use the convertToSchedulingPriority
function to translate a normalized, floating-point priority in the range [ 0.0, 1.0 ]
to an integer priority in the range [ getMinSchedulingPriority, getMaxSchedulingPriority ]
to set the schedulingPriority
attribute.
typedef void(* bslmt_KeyDestructorFunction) (void *) |
bslmt_KeyDestructorFunction
is an alias for a function type taking a single void
pointer argument and returning void
. Such functions are suitable to be specified as thread-specific key destructor functions to bslmt::ThreadUtil::createKey
.
typedef void *(* bslmt_ThreadFunction) (void *) |
bslmt_ThreadFunction
is an alias for a function type taking a single void
pointer argument and returning void *
. Such functions are suitable to be specified as thread entry-point functions to bslmt::ThreadUtil::create
. Note that create
also accepts any invokable C++ "functor" object.