BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslmt.h
Go to the documentation of this file.
1/// @file bslmt.h
2///
3///
4/// @defgroup bslmt Package bslmt
5/// @brief Basic Standard Library Multi-Threading (bslmt)
6/// @addtogroup bsl
7/// @{
8/// @addtogroup bslmt
9/// @{
10/// * <a href="#bslmt-purpose"> Purpose</a>
11/// * <a href="#bslmt-mnemonic"> Mnemonic </a>
12/// * <a href="#bslmt-description"> Description </a>
13/// * <a href="#bslmt-hierarchical-synopsis"> Hierarchical Synopsis </a>
14/// * <a href="#bslmt-component-synopsis"> Component Synopsis </a>
15/// * <a href="#bslmt-thread-management"> Thread Management </a>
16/// * <a href="#bslmt-thread-synchronization"> Thread Synchronization </a>
17/// * <a href="#bslmt-basic-mutexes-bslmt-mutex-and-bslmt-recursivemutex"> Basic Mutexes: bslmt::Mutex and bslmt::RecursiveMutex </a>
18/// * <a href="#bslmt-inter-thread-condition-variables-bslmt-condition"> Inter-thread Condition Variables: bslmt::Condition </a>
19/// * <a href="#bslmt-lockingunlocking-critical-code"> LockingUnlocking Critical Code </a>
20/// * <a href="#bslmt-thread-specific-storage"> Thread-Specific Storage </a>
21/// * <a href="#bslmt-synchronization-of-multiple-parallel-threads-bslmt-barrier"> Synchronization of Multiple, Parallel Threads: bslmt::Barrier </a>
22/// * <a href="#bslmt-bslmt-readwrite-locking-components"> bslmt readwrite Locking Components </a>
23/// * <a href="#bslmt-recursive-write-locks-bslmt-recursiverwlock"> Recursive Write Locks: bslmt::RecursiveRWLock </a>
24/// * <a href="#bslmt-static-initialization"> Static Initialization </a>
25/// * <a href="#bslmt-implementation-classes-and-components"> Implementation Classes and Components </a>
26///
27/// # Purpose {#bslmt-purpose}
28/// Support for multi-threading and thread-safe processes.
29///
30/// # Mnemonic {#bslmt-mnemonic}
31/// Basic Standard Library Multi-Threading (bslmt)
32///
33/// # Description {#bslmt-description}
34/// The 'bslmt' package supports primitives that allow creation and
35/// management of operating system threads. Using the components in 'bslmt',
36/// clients can write efficient, thread-safe code.
37///
38/// Thread creation and management of mutually exclusive locks (mutexes) are
39/// supported in a way that should be familiar to developers with experience
40/// programming in threads on any common computing platform. In addition,
41/// individual components such as @ref bslmt_lockguard and @ref bslmt_barrier manage
42/// synchronization concerns through simple, idiomatic object interfaces that make
43/// client code easy to read and understand.
44///
45/// ## Hierarchical Synopsis {#bslmt-hierarchical-synopsis}
46///
47/// The 'bslmt' package currently has 51 components having 17 levels of physical
48/// dependency. The list below shows the hierarchical ordering of the components.
49/// The order of components within each level is not architecturally significant,
50/// just alphabetical.
51/// @code
52/// 17. bslmt_once
53/// bslmt_readerwriterlockassert
54/// bslmt_rwmutex !DEPRECATED!
55///
56/// 16. bslmt_fastpostsemaphore
57/// bslmt_latch
58/// bslmt_meteredmutex
59/// bslmt_qlock
60/// bslmt_readerwriterlock
61/// bslmt_readerwritermutex
62/// bslmt_throughputbenchmark
63///
64/// 15. bslmt_barrier
65/// bslmt_fastpostsemaphoreimpl
66/// bslmt_readerwritermuteximpl
67/// bslmt_turnstile
68///
69/// 14. bslmt_timedcompletionguard
70///
71/// 13. bslmt_condition
72///
73/// 12. bslmt_conditionimpl_win32 !PRIVATE!
74///
75/// 11. bslmt_sluice
76/// bslmt_threadgroup
77///
78/// 10. bslmt_semaphore
79///
80/// 9. bslmt_semaphoreimpl_counted !PRIVATE!
81/// bslmt_timedsemaphore
82///
83/// 8. bslmt_conditionimpl_pthread !PRIVATE!
84/// bslmt_mutexassert
85/// bslmt_semaphoreimpl_darwin !PRIVATE!
86/// bslmt_semaphoreimpl_pthread !PRIVATE!
87/// bslmt_semaphoreimpl_win32 !PRIVATE!
88/// bslmt_testutil
89/// bslmt_timedsemaphoreimpl_posixadv !PRIVATE!
90/// bslmt_timedsemaphoreimpl_pthread !PRIVATE!
91/// bslmt_timedsemaphoreimpl_win32 !PRIVATE!
92///
93/// 7. bslmt_mutex
94/// bslmt_recursivemutex
95///
96/// 6. bslmt_threadutil
97///
98/// 5. bslmt_entrypointfunctoradapter
99///
100/// 4. bslmt_threadutilimpl_pthread !PRIVATE!
101/// bslmt_threadutilimpl_win32 !PRIVATE!
102///
103/// 3. bslmt_configuration
104/// bslmt_recursivemuteximpl_win32 !PRIVATE!
105///
106/// 2. bslmt_muteximpl_pthread !PRIVATE!
107/// bslmt_muteximpl_win32 !PRIVATE!
108/// bslmt_recursivemuteximpl_pthread !PRIVATE!
109/// bslmt_saturatedtimeconversionimputil
110/// bslmt_threadattributes
111///
112/// 1. bslmt_chronoutil
113/// bslmt_lockguard
114/// bslmt_platform
115/// bslmt_readlockguard
116/// bslmt_threadlocalvariable
117/// bslmt_throughputbenchmarkresult
118/// bslmt_writelockguard
119/// @endcode
120///
121/// ## Component Synopsis {#bslmt-component-synopsis}
122///
123/// @ref bslmt_barrier :
124/// Provide a thread barrier component.
125///
126/// @ref bslmt_chronoutil :
127/// Provide utilities related to threading with C++11-style clocks.
128///
129/// @ref bslmt_condition :
130/// Provide a portable, efficient condition variable.
131///
132/// @ref bslmt_conditionimpl_pthread : !PRIVATE!
133/// Provide a POSIX implementation of `bslmt::Condition`.
134///
135/// @ref bslmt_conditionimpl_win32 : !PRIVATE!
136/// Provide a win32 implementation of `bslmt::Condition`.
137///
138/// @ref bslmt_configuration :
139/// Provide utilities to allow configuration of values for BCE.
140///
141/// @ref bslmt_entrypointfunctoradapter :
142/// Provide types and utilities to simplify thread creation.
143///
144/// @ref bslmt_fastpostsemaphore :
145/// Provide a semaphore class optimizing `post`.
146///
147/// @ref bslmt_fastpostsemaphoreimpl :
148/// Provide a testable semaphore class optimizing `post`.
149///
150/// @ref bslmt_latch :
151/// Provide a single-use mechanism for synchronizing on an event count.
152///
153/// @ref bslmt_lockguard :
154/// Provide generic scoped guards for synchronization objects.
155///
156/// @ref bslmt_meteredmutex :
157/// Provide a mutex capable of keeping track of wait and hold time.
158///
159/// @ref bslmt_mutex :
160/// Provide a platform-independent mutex.
161///
162/// @ref bslmt_mutexassert :
163/// Provide an assert macro for verifying that a mutex is locked.
164///
165/// @ref bslmt_muteximpl_pthread : !PRIVATE!
166/// Provide a POSIX implementation of `bslmt::Mutex`.
167///
168/// @ref bslmt_muteximpl_win32 : !PRIVATE!
169/// Provide a win32 implementation of `bslmt::Mutex`.
170///
171/// @ref bslmt_once :
172/// Provide a thread-safe way to execute code once per process.
173///
174/// @ref bslmt_platform :
175/// Provide platform-dependent thread-related trait definitions.
176///
177/// @ref bslmt_qlock :
178/// Provide small, statically-initializable mutex lock.
179///
180/// @ref bslmt_readerwriterlock :
181/// Provide a multi-reader/single-writer lock.
182///
183/// @ref bslmt_readerwriterlockassert :
184/// Provide an assert macro for verifying reader-writer lock status.
185///
186/// @ref bslmt_readerwritermutex :
187/// Provide a multi-reader/single-writer lock.
188///
189/// @ref bslmt_readerwritermuteximpl :
190/// Provide a multi-reader/single-writer lock.
191///
192/// @ref bslmt_readlockguard :
193/// Provide generic scoped guards for read synchronization objects.
194///
195/// @ref bslmt_recursivemutex :
196/// Provide a platform-independent recursive mutex.
197///
198/// @ref bslmt_recursivemuteximpl_pthread : !PRIVATE!
199/// Provide a POSIX implementation of `bslmt::RecursiveMutex`.
200///
201/// @ref bslmt_recursivemuteximpl_win32 : !PRIVATE!
202/// Provide a win32 implementation of `bslmt::RecursiveMutex`.
203///
204/// @ref bslmt_rwmutex : !DEPRECATED!
205/// Provide a platform-independent RW mutex class.
206///
207/// @ref bslmt_saturatedtimeconversionimputil :
208/// Provide special narrowing conversions for time types.
209///
210/// @ref bslmt_semaphore :
211/// Provide a semaphore class.
212///
213/// @ref bslmt_semaphoreimpl_counted : !PRIVATE!
214/// Provide an implementation of `bslmt::Semaphore` with count.
215///
216/// @ref bslmt_semaphoreimpl_darwin : !PRIVATE!
217/// Provide a Darwin implementation of `bslmt::Semaphore`.
218///
219/// @ref bslmt_semaphoreimpl_pthread : !PRIVATE!
220/// Provide a POSIX implementation of `bslmt::Semaphore`.
221///
222/// @ref bslmt_semaphoreimpl_win32 : !PRIVATE!
223/// Provide a win32 implementation of `bslmt::Semaphore`.
224///
225/// @ref bslmt_sluice :
226/// Provide a "sluice" class.
227///
228/// @ref bslmt_testutil :
229/// Provide thread-safe test utilities for multithreaded components.
230///
231/// @ref bslmt_threadattributes :
232/// Provide a description of the attributes of a thread.
233///
234/// @ref bslmt_threadgroup :
235/// Provide a container for managing a group of threads.
236///
237/// @ref bslmt_threadlocalvariable :
238/// Provide a macro to declare a thread-local variable.
239///
240/// @ref bslmt_threadutil :
241/// Provide platform-independent utilities related to threading.
242///
243/// @ref bslmt_threadutilimpl_pthread : !PRIVATE!
244/// Provide a POSIX implementation of `bslmt::ThreadUtil`.
245///
246/// @ref bslmt_threadutilimpl_win32 : !PRIVATE!
247/// Provide a win32 implementation of `bslmt::ThreadUtil`.
248///
249/// @ref bslmt_throughputbenchmark :
250/// Provide a performance test harness for multi-threaded components.
251///
252/// @ref bslmt_throughputbenchmarkresult :
253/// Provide result repository for throughput performance test harness.
254///
255/// @ref bslmt_timedcompletionguard :
256/// Provide guard to verify work completion within a set duration.
257///
258/// @ref bslmt_timedsemaphore :
259/// Provide a timed semaphore class.
260///
261/// @ref bslmt_timedsemaphoreimpl_posixadv : !PRIVATE!
262/// Provide "advanced" POSIX implementation of `bslmt::TimedSemaphore`.
263///
264/// @ref bslmt_timedsemaphoreimpl_pthread : !PRIVATE!
265/// Provide a POSIX implementation of `bslmt::TimedSemaphore`.
266///
267/// @ref bslmt_timedsemaphoreimpl_win32 : !PRIVATE!
268/// Provide a win32 implementation of `bslmt::TimedSemaphore`.
269///
270/// @ref bslmt_turnstile :
271/// Provide a mechanism to meter time.
272///
273/// @ref bslmt_writelockguard :
274/// Provide generic scoped guards for write synchronization objects.
275///
276/// ## Thread Management {#bslmt-thread-management}
277///
278/// Thread management is done via the utility class 'bslmt::ThreadUtil' in the
279/// @ref bslmt_threadutil component. This component presents a platform-independent
280/// protocol whose operations nonetheless correspond closely to native operations
281/// on platform-specific threads. 'bslmt::ThreadUtil' presents a procedural
282/// interface -- that is, clients do not instantiate 'bslmt::ThreadUtil' objects,
283/// and all methods of 'bslmt::ThreadUtil' are declared 'static'.
284///
285/// Clients create threads with the functions
286/// 'bslmt::ThreadUtil::create(Handle*, ...)'. On success, these functions yield
287/// a 'bslmt::ThreadUtil::Handle' object that provides a lightweight handle for an
288/// operating systems thread. Similarly, clients destroy threads for which they
289/// have a current 'Handle' with 'bslmt::ThreadUtil::terminate(Handle*)'.
290///
291/// 'bslmt::ThreadUtil' supports two 'create()' functions. The first function
292/// simply takes a pointer to a 'bslmt::ThreadUtil::Handle'; the second function
293/// takes a 'bslmt::ThreadAttributes' object that defines component-specific and
294/// platform-specific attributes of the created thread. Platform-specific
295/// attributes include scheduling policy and priority, and thread stack size. See
296/// the @ref bslmt_threadattributes component documentation for details.
297///
298/// ## Thread Synchronization {#bslmt-thread-synchronization}
299///
300/// The 'bslmt' package contains several components and classes that support
301/// thread synchronization in a number of ways.
302///
303/// ### Basic Mutexes: bslmt::Mutex and bslmt::RecursiveMutex {#bslmt-basic-mutexes-bslmt-mutex-and-bslmt-recursivemutex}
304///
305/// At the lowest level, synchronization between threads is done with
306/// 'bslmt::Mutex', a type defined in component @ref bslmt_mutex . Locks are
307/// acquired with 'bslmt::Mutex::lock()' or 'bslmt::Mutex::tryLock()', and
308/// released with 'bslmt::Mutex::unlock()'. The 'bslmt' package provides several
309/// other mechanisms for synchronization and communication, which suit specific
310/// synchronization problems and allow more idiomatic C++ coding.
311///
312/// 'bslmt::RecursiveMutex' is similar to 'bslmt::Mutex', except that it can be
313/// 'lock'ed multiple times *in* *a* *single* *thread*. A corresponding number of
314/// calls to 'unlock()' are required to unlock the mutex. 'bslmt::RecursiveMutex'
315/// is defined in component @ref bslmt_recursivemutex .
316///
317/// ### Inter-thread Condition Variables: bslmt::Condition {#bslmt-inter-thread-condition-variables-bslmt-condition}
318///
319/// The 'bslmt::Condition' class, defined in component @ref bslmt_condition ,
320/// implements a "condition variable" that can be used by multiple threads to
321/// communicate changes in the condition. Multiple threads wait on the condition
322/// by calling 'myCondition->wait(bslmt::Mutex*)', suspending their execution and
323/// waiting. An individual thread then signals when the condition is met by
324/// calling either 'myCondition->signal()' (waking up a single waiting thread) or
325/// 'myCondition->broadcast()' (waking up all waiting threads). Waits with
326/// timeouts are supported through 'bslmt::Condition::timedWait()'.
327///
328/// ### LockingUnlocking Critical Code {#bslmt-lockingunlocking-critical-code}
329///
330/// Code in multiple threads can create a 'bslmt::Mutex' and call 'mutex->lock()'
331/// and 'mutex->unlock()' to guarantee exclusive access to critical sections of
332/// code. A more idiomatic usage is provided by 'bslmt::LockGuard' and
333/// 'bslmt::LockGuardUnlock'; objects of these types themselves lock and unlock a
334/// 'bslmt::Mutex' when they are created or destroyed, allowing clients to manage
335/// mutex locks in a way more transparent, safer way, and also making sure the
336/// lock is released when an exception is thrown within the critical section.
337/// @code
338/// static void myThreadsafeFunction(myObject *obj, bslmt::Mutex *mutex)
339/// {
340/// // creating the 'LockGuard', 'guard', calls 'mutex->lock()'
341/// bslmt::LockGuard<bslmt::Mutex> guard(mutex);
342/// if (someCondition) {
343/// obj->someMethod();
344/// return;
345/// // 'guard' is destroyed; this calls 'mutex->unlock()'
346/// } else if (someOtherCondition) {
347/// obj->someOtherMethod();
348/// return;
349/// // 'guard' is destroyed; this calls 'mutex->unlock()'
350/// }
351/// obj->defaultMethod();
352/// return;
353/// // 'guard' is destroyed; this calls 'mutex->unlock()'
354/// }
355/// @endcode
356///
357/// ### Thread-Specific Storage {#bslmt-thread-specific-storage}
358///
359/// Thread-specific storage (also known as thread-local storage, or TLS) is a
360/// mechanism for referring to a single variable (identified by its 'Key' in
361/// 'bslmt::ThreadUtil', provided by the @ref bslmt_threadutil component) whose
362/// content varies according to the thread it is observed in. This is useful for
363/// status variables such as 'errno', which is implemented by the system as a TLS,
364/// since otherwise it would be impossible to use 'errno' values in a thread, not
365/// knowing if another thread had modified it after the last system call in this
366/// thread.
367///
368/// ### Synchronization of Multiple, Parallel Threads: bslmt::Barrier {#bslmt-synchronization-of-multiple-parallel-threads-bslmt-barrier}
369///
370/// The @ref bslmt_barrier component provides a single class, 'bslmt::Barrier', that
371/// can be used to synchronize processing among multiple threads. The
372/// 'bslmt::Barrier' is created with a single argument specifying the number of
373/// threads to synchronize. Each individual thread then calls 'myBarrier->wait()'
374/// when it reaches its synchronization point. All waiting threads are blocked
375/// until the required number of threads have called 'myBarrier->wait()'; once the
376/// required number is reached, the threads are released and can continue
377/// processing.
378///
379/// ### bslmt readwrite Locking Components {#bslmt-bslmt-readwrite-locking-components}
380///
381/// There are 3 components that provide locking mechanisms that allows multiple
382/// threads to simultaneously lock for read, while providing exclusive access to
383/// a thread locking for write.
384///
385/// * 'bslmt::ReaderWriterMutex': Preferred for most use-cases, has been shown to
386/// be faster than 'bslmt::ReaderWriterLock': under most conditions and is
387/// generally the best choice.
388///
389/// * 'bslmt::ReaderWriterLock': Preferred only when very long hold times are
390/// anticipated. It also provides 'upgrade*' methods from a locked-for-read
391/// state to a locked-for-write state, but the use of this feature is
392/// discouraged as it has performed poorly on benchmarks.
393///
394/// * 'bslmt::RWMutex': Deprecated.
395///
396/// Note that for extremely short hold times and very high concurrency, a
397/// 'bslmt::Mutex' might outperform all of the above.
398///
399/// Also note that reader/writer locks also have their own guards, provided by the
400/// templated 'bslmt::ReadLockGuard' and 'bslmt::WriteLockGuard' classes, which
401/// work on locks of all 3 types.
402///
403/// Also note that assertions to verify locking are available from
404/// @ref bslmt_readerwriterlockassert , which work on locks of type
405/// 'bslmt::ReaderWriterMutex' and 'bslmt::ReaderWriterLock', but not
406/// 'bslmt::RWMutex'.
407///
408/// ### Recursive Write Locks: bslmt::RecursiveRWLock {#bslmt-recursive-write-locks-bslmt-recursiverwlock}
409///
410/// This component is *DEPRECATED*. It can be emulated by a wrapper on a
411/// 'bslmt::ReaderWriterLock' along with a thread-specific counter used to store
412/// the number of recursive locks acquired by the current thread.
413///
414/// ### Static Initialization {#bslmt-static-initialization}
415///
416/// Initialization of a thread-specific static variable (such as a thread-local
417/// singleton) can be achieved by using the @ref bslmt_once component. The
418/// @ref bslmt_qlock component ensures thread-safe static initialization of shared
419/// resources by providing a light-weight, statically initializable lock. Note
420/// that the classic 'bslmt::Mutex' provided by the @ref bslmt_mutex component
421/// cannot safely be used in static initializations because some platforms (e.g.,
422/// Windows XP) do not have a native statically-initializable mutex type.
423///
424/// ## Implementation Classes and Components {#bslmt-implementation-classes-and-components}
425///
426/// The 'bslmt' package is supported on all Bloomberg platforms. In order to
427/// accomplish this, 'bslmt' provides platform-specific implementations of thread
428/// primitives. The following components provide platform-specific
429/// implementations:
430/// @code
431/// Component | Purpose
432/// +==============================+===========================================+
433/// | bslmt_threadutilimpl_pthread | Template specialization for POSIX threads |
434/// | bslmt_threadutilimpl_win32 | Template specialization for MS-Windows |
435/// +------------------------------+-------------------------------------------+
436/// @endcode
437/// These components are visible and documented. However, their intended use is
438/// to support component @ref bslmt_threadutil . Clients should not expect to use
439/// them directly.
440///
441/// @}
442/** @} */