ConditionSynchronization.h
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarXCore
19 * @author Kai Welke (welke at kit dot edu)
20 * @date 2013
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24
25#pragma once
26
27#if 0 // I think this header is no longer used (?)
28
29#include <boost/thread.hpp>
30
31namespace armarx
32{
33
34 /**
35 * @class ConditionSynchronization
36 * @ingroup Threads
37 * Encapsules the synchronization members for multiple purposes.
38 * The synchronization uses a single condition, protected by a mutex.
39 * Typical usage is to transfer data from one thread to another
40 * with a signal (producer / consumer).
41 *
42 * Example usage in producer / consumer:
43 * @code
44 * ConditionSynchronization sync;
45 * int sharedData;
46 *
47 * int consumer_thread()
48 * {
49 * while(true)
50 * {
51 * sync.lockNotify();
52 * sharedData = rand();
53 * sync.notify();
54 * sync.unlockNotify();
55 * }
56 *
57 * consumerSync.abort();
58 * }
59 *
60 * int producer_thread()
61 * {
62 * bool abort = false;
63 * while(!abort)
64 * {
65 * consumerSync.lockWait();
66 * abort = consumerSync.wait();
67 * std::cout << "Got " << sharedData << std::endl
68 * consumerSync.unlockWait();
69 * }
70 * }
71 * @endcode
72 **/
73 class ConditionSynchronization
74 {
75 public:
76 /**
77 * Initializes all synchronization objects
78 **/
79 ConditionSynchronization()
80 {
81 reset();
82
83 // create two locks, do not lock
84 condLockWait = boost::unique_lock<boost::mutex>(condMutex, boost::defer_lock_t());
85 condLockNotify = boost::unique_lock<boost::mutex>(condMutex, boost::defer_lock_t());
86 }
87
88 void reset()
89 {
90 // work around spurious wakeups
91 condition = false;
92 abortRequested = false;
93 }
94
95 /**
96 * Locks the mutex for waiting. Use this before calling ConditionSynchronization::wait().
97 **/
98 void lockWait()
99 {
100 condLockWait.lock();
101 }
102
103 /**
104 * Unlocks the mutex after waiting. Use this after calling ConditionSynchronization::wait().
105 **/
106 void unlockWait()
107 {
108 condLockWait.unlock();
109 }
110
111 /**
112 * Locks the mutex for notifying. Use this before calling ConditionSynchronization::notify().
113 **/
114 void lockNotify()
115 {
116 condLockNotify.lock();
117 }
118
119 /**
120 * Unlocks the mutex after notifying. Use this after calling ConditionSynchronization::notify().
121 **/
122 void unlockNotify()
123 {
124 condLockNotify.unlock();
125 }
126
127 /**
128 * Notifies the condition
129 **/
130 void notify()
131 {
132 condition = true;
133 condVar.notify_one();
134 }
135
136 /**
137 * Waits for condition condition
138 *
139 * @return false if abort has been requested, true otherwise
140 **/
141 bool wait()
142 {
143 while (!condition)
144 {
145 condVar.wait(condLockWait);
146 }
147
148 condition = false;
149
150 return !abortRequested;
151 }
152
153 /**
154 * Aborts the synchronization. Results in notifying the condition, thus waking up a waiting thread. The abort is indicated via the return
155 * value of wait.
156 **/
157 void abort()
158 {
159 lockNotify();
160 abortRequested = true;
161 notify();
162 unlockNotify();
163 }
164
165 private:
166 boost::mutex condMutex;
167 boost::unique_lock<boost::mutex> condLockWait;
168 boost::unique_lock<boost::mutex> condLockNotify;
169 boost::condition_variable condVar;
170 bool condition;
171 bool abortRequested;
172 };
173
174}
175
176#endif
This file offers overloads of toIce() and fromIce() functions for STL container types.