BloombergLP::bslmt::ThreadUtil

This struct provides a suite of portable utility functions for managing threads.

Synopsis

Declared in <bslmt_threadutil.h>

struct ThreadUtil;

Type Aliases

NameDescription
Destructor Prototype for thread-specific key destructors.
Handle Thread handle type. Use this type to refer to a thread in a platform-independent way.
Id Thread identifier type - distinguished from a Handle in that it does not have any resources associated with it, whereas Handle may, depending on platform.
Imp Platform-specific implementation type.
Key Thread-specific key type, used to refer to thread-specific storage.
NativeHandle Platform-specific thread handle type.
ThreadFunction Prototype for thread entry-point functions.

Static Member Functions

NameDescription
areEqual Return true if the specified a and b thread handles identify the same thread, or if both a and b are invalid handles, and false otherwise. Note that if either of a or b is an invalid handle, but not both, this method returns false.
areEqualId Return true if the specified a thread identifier is associated with the same thread as the specified b thread identifier, and false otherwise.
convertToSchedulingPriority Return an integer scheduling priority appropriate for the specified normalizedSchedulingPriority and the specified policy. If either the minimum or maximum priority for this platform cannot be determined, return ThreadAttributes::e_UNSET_PRIORITY. Higher values of normalizedSchedulingPriority are considered to represent more urgent priorities. The behavior is undefined unless policy is a valid ThreadAttributes::SchedulingPolicy and normalizedSchedulingPriority is in the range [ 0.0, 1.0 ].
create create overloads
createKey Load into the specified key a new process-wide identifier that can be used to store (via setSpecific) and retrieve (via getSpecific) a pointer value local to each thread, and associate with the new key the specified threadKeyCleanupFunction, which will be called by each thread, if threadKeyCleanupFunction is non-zero and the value associated with key for that thread is non-zero, with the associated value as an argument, after the function passed to create has returned and before the thread terminates. Return 0 on success, and a non-zero value otherwise. Note that multiple keys can be defined, which can result in multiple thread key cleanup functions being called for a given thread.
createWithAllocator createWithAllocator overloads
deleteKey Delete the specified key from the calling process, and disassociate all threads from the thread key cleanup function supplied when key was created (see createKey). Return 0 on success, and a non-zero value otherwise. The behavior is undefined unless key was obtained from a successful call to createKey and has not already been deleted. Note that deleting a key does not delete any data referred to by the pointer values associated with that key in any thread.
detach "Detach" the thread identified by the specified handle such that when it terminates, the resources associated with that thread will automatically be reclaimed. The behavior is undefined unless handle was obtained by a call to create or self. Note that once a thread is "detached", it is no longer possible to join the thread to retrieve its exit status.
exit Exit the current thread and return the specified status. If the current thread is not "detached", then a call to join must be made to reclaim any resources used by the thread, and to retrieve the exit status. Note that the preferred method of exiting a thread is to return from the entry point function.
getMaxSchedulingPriority Return the maximum available priority for the specified policy, where policy is of type ThreadAttributes::SchedulingPolicy. Return ThreadAttributes::e_UNSET_PRIORITY if the maximum scheduling priority cannot be determined. Note that, for some platform / policy combinations, getMinSchedulingPriority(policy) and getMaxSchedulingPriority(policy) return the same value.
getMinSchedulingPriority Return the minimum available priority for the specified policy, where policy is of type ThreadAttributes::SchedulingPolicy. Return ThreadAttributes::e_UNSET_PRIORITY if the minimum scheduling priority cannot be determined. Note that, for some platform / policy combinations, getMinSchedulingPriority(policy) and getMaxSchedulingPriority(policy) return the same value.
getSpecific Return the thread-local value associated with the specified key. A key is shared among all threads and the value associated with key for each thread is 0 until it is set by that thread using setSpecific. The behavior is undefined unless this method is called outside any thread key cleanup function associated with any key by createKey, key was obtained from a successful call to createKey, and key has not been deleted.
getThreadName Load the name of the current thread into the specified *threadName. Note that this method clears *threadName on platforms other than Linux, Solaris, Darwin, and Windows.
handleToId Return the unique identifier of the thread having the specified threadHandle within the current process. The behavior is undefined unless handle was obtained by a call to create or self. Note that this value is valid only until the thread terminates, and may be reused thereafter.
hardwareConcurrency Return a hint at the number of concurrent threads supported by this platform on success, and 0 otherwise.
idAsInt Return the unique integral identifier of a thread uniquely identified by the specified threadId within the current process. Note that this representation is particularly useful for logging purposes. Also note that this value is only valid until the thread terminates and may be reused thereafter.
idAsUint64 Return the unique integral identifier of a thread uniquely identified by the specified threadId within the current process. Note that this representation is particularly useful for logging purposes. Also note that this value is only valid until the thread terminates and may be reused thereafter.
invalidHandle Return a reference to the non-modifiable Handle object that is guaranteed never to be a valid thread handle.
isEqual Return true if the specified a and b thread handles identify the same thread, or if both a and b are invalid handles, and false otherwise. Note that if either of a or b is an invalid handle, but not both, this method returns false.
isEqualId Return true if the specified lhs thread identifier is associated with the same thread as the specified rhs thread identifier, and false otherwise.
isThreadLimitReached Counts down the limit counter set by ThreadUtil::setThreadLimit. Returns true if current thread creation attempt should be failed. Note that if the thread limit is reached, all future thread creation attempts will fail, unless the limit is reset by a call to setThreadLimit.
join Suspend execution of the current thread until the thread referred to by the specified threadHandle terminates, and reclaim any system resources associated with threadHandle. Return 0 on success, and a non-zero value otherwise. If the optionally specified status is not 0, load into *status the value returned by the function supplied at the creation of the thread identified by threadHandle. The behavior is undefined unless threadHandle was obtained by a call to create.
microSleep Suspend execution of the current thread for a period of at least the specified microseconds and the optionally specified seconds (relative time), or an interrupting signal is received. Note that the actual time suspended depends on many factors including system scheduling and system timer resolution, and may be significantly longer than the time requested.
nativeHandle Return the platform-specific identifier associated with the thread referred to by the specified handle. The behavior is undefined unless handle was obtained by a call to create or self. Note that the returned native handle may not be a globally unique identifier for the thread (see selfIdAsUint).
self Return an opaque thread identifier that can be used to refer to the current thread in calls to other ThreadUtil methods. Note that identifier may only be used to refer to the current thread from the current thread (the handle returned is not valid in other threads).
selfId Return an identifier that can be used to uniquely identify the current thread within the current process. Note that the identifier is only valid until the thread terminates and may be reused thereafter.
selfIdAsInt Return an integral identifier that can be used to uniquely identify the current thread within the current process. Note that this representation is particularly useful for logging purposes. Also note that this value is only valid until the thread terminates and may be reused thereafter.
selfIdAsUint64 Return an integral identifier that can be used to uniquely identify the current thread within the current process. Note that this representation is particularly useful for logging purposes. Also note that this value is valid only until the thread terminates, and may be reused thereafter.
selfKernelIdAsUint64 Return an integral identifier that can be used to uniquely identify the kernel thread within the current process. Note that this identifier may be different from the identifier returned by selfIdAsUint64(). Also note that this value is valid only until the thread terminates, and may be reused thereafter. Also note that this method returns the kernel thread id only on the operating systems which implement that (on Windows, the thread ids returned by both methods are the same).
setSpecific Associate the specified thread-local value with the specified process-wide key. Return 0 on success, and a non-zero value otherwise. The value associated with a thread for a given key is 0 until it has been set by that thread using setSpecific. The behavior is undefined unless this method is called outside any thread key cleanup function associated with any key by createKey, key was obtained from a successful call to createKey, and key has not been deleted.
setThreadLimit Enable a thread creation limit. If set to non-zero value n'th thread creation will fail. The limit is disabled by setting the limit to 0. Only intended to be used in tests to inject thread creation errors. Note that this can be called multiple times to reset the thread limit, e.g. between subtests for example.
setThreadName Set the name of the current thread to the specified threadName. On platforms other than Linux, Solaris, Darwin and Windows this method has no effect. Note that on those two platforms threadName will be truncated to a length of 15 bytes, not including the terminating '0'.
sleep Suspend execution of the current thread for a period of at least the specified (relative) sleepTime, or an interrupting signal is received. Note that the actual time suspended depends on many factors including system scheduling and system timer resolution.
sleepUntil sleepUntil overloads
yield Move the current thread to the end of the scheduler's queue and schedule another thread to run. Note that this allows cooperating threads of the same priority to share CPU resources equally.