9 #ifndef __OGLPLUS_EXAMPLE_EXAMPLE_MT_1119071146_HPP__
10 #define __OGLPLUS_EXAMPLE_EXAMPLE_MT_1119071146_HPP__
16 #include <condition_variable>
21 class ExampleSyncQueue
24 std::queue<Sync> sync_queue;
25 std::mutex sync_mutex;
26 std::condition_variable sync_cv;
29 ExampleSyncQueue(
void)
33 ExampleSyncQueue(ExampleSyncQueue&& tmp)
34 : sync_queue(std::move(tmp.sync_queue))
37 , cancelled(tmp.cancelled)
42 ExampleSyncQueue& operator = (ExampleSyncQueue&& tmp)
44 sync_queue = std::move(tmp.sync_queue);
45 cancelled = tmp.cancelled;
50 ~ExampleSyncQueue(
void)
57 std::unique_lock<std::mutex> l(sync_mutex);
61 std::queue<Sync> empty;
62 std::swap(sync_queue, empty);
67 void Push(Sync&& sync)
69 std::unique_lock<std::mutex> l(sync_mutex);
70 sync_queue.emplace(std::move(sync));
81 std::unique_lock<std::mutex> l(sync_mutex);
82 while(sync_queue.empty() && !cancelled) sync_cv.wait(l);
83 Sync tmp(cancelled?
Sync():std::move(sync_queue.front()));
84 if(!cancelled) sync_queue.pop();
85 return std::move(tmp);
94 class ExampleFrameSyncQueue
97 std::mutex sync_mutex;
98 std::condition_variable sync_cv;
99 std::queue<Sync> sync_queue;
100 std::queue<GLuint> value_queue;
103 ExampleFrameSyncQueue(
void)
107 ExampleFrameSyncQueue(ExampleFrameSyncQueue&& tmp)
110 , sync_queue(std::move(tmp.sync_queue))
111 , value_queue(std::move(tmp.value_queue))
112 , cancelled(tmp.cancelled)
114 tmp.cancelled =
true;
117 ExampleFrameSyncQueue& operator = (ExampleFrameSyncQueue&& tmp)
119 sync_queue = std::move(tmp.sync_queue);
120 value_queue = std::move(tmp.value_queue);
121 cancelled = tmp.cancelled;
122 tmp.cancelled =
true;
126 ~ExampleFrameSyncQueue(
void)
133 std::unique_lock<std::mutex> l(sync_mutex);
137 std::queue<Sync> tsq;
138 std::queue<GLuint> tvq;
139 std::swap(sync_queue, tsq);
140 std::swap(value_queue, tvq);
141 sync_cv.notify_all();
145 void Push(GLuint value)
147 std::unique_lock<std::mutex> l(sync_mutex);
148 sync_queue.push(
Sync());
149 value_queue.push(value);
150 sync_cv.notify_all();
155 std::unique_lock<std::mutex> l(sync_mutex);
156 while(!cancelled && sync_queue.empty()) sync_cv.wait(l);
160 sync_queue.front().Wait();
163 GLuint result = value_queue.front();
173 #endif // include guard