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