3 #ifndef CDSLIB_SYNC_MONITOR_H
4 #define CDSLIB_SYNC_MONITOR_H
6 #include <cds/details/defs.h>
8 namespace cds { namespace sync {
11 @page cds_sync_monitor Synchronization monitor
13 A <a href="http://en.wikipedia.org/wiki/Monitor_%28synchronization%29">monitor</a> is synchronization construct
14 that allows threads to have both mutual exclusion and the ability to wait (block) for a certain condition to become true.
16 Some blocking data structure algoritms like the trees require per-node locking.
17 For huge trees containing millions of nodes it can be very inefficient to inject
18 the lock object into each node. Moreover, some operating systems may not support
19 the millions of system objects like mutexes per user process.
21 The monitor strategy is intended to solve that problem.
22 When the node should be locked, the monitor is called to allocate appropriate
23 lock object for the node if it's needed, and to lock the node.
24 The monitor strategy can significantly reduce the number of system objects
25 required for the data structure.
29 \p libcds contains several monitor implementations:
30 - \p sync::injecting_monitor injects the lock object into each node.
31 That mock monitor is designed for user-space locking primitive like
32 \ref sync::spin_lock "spin-lock".
36 If you use a container from \p libcds that requires a monitor, you should just
37 specify required monitor type in container's traits. Usually, the monitor
38 is specified by \p traits::sync_monitor typedef, or by \p cds::opt::sync_monitor
39 option for container's \p make_traits metafunction.
41 If you're developing a new container algorithm, you should know internal monitor
46 // Monitor's injection into the Node class
47 template <typename Node>
51 template <typename Node>
52 void lock( Node& node );
55 template <typename Node>
56 void unlock( Node& node );
58 // Scoped lock applyes RAII to Monitor
59 template <typename Node>
63 // Locks node by monitor mon
64 scoped_lock( Monitor& mon, Node& node );
66 // Unlocks the node locked by ctor
71 The monitor should be a member of your container:
73 template <typename GC, typename T, typename Traits>
76 typedef typename Traits::sync_monitor sync_monitor;
77 sync_monitor m_Monitor;
83 }} // namespace cds::sync
85 #endif // #ifndef CDSLIB_SYNC_MONITOR_H