Audio Processing Framework (APF) version 0.5.0
threadtools.h
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright © 2012-2014 Institut für Nachrichtentechnik, Universität Rostock *
3 * Copyright © 2006-2012 Quality & Usability Lab, *
4 * Telekom Innovation Laboratories, TU Berlin *
5 * *
6 * This file is part of the Audio Processing Framework (APF). *
7 * *
8 * The APF is free software: you can redistribute it and/or modify it under *
9 * the terms of the GNU General Public License as published by the Free *
10 * Software Foundation, either version 3 of the License, or (at your option) *
11 * any later version. *
12 * *
13 * The APF is distributed in the hope that it will be useful, but WITHOUT ANY *
14 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS *
15 * FOR A PARTICULAR PURPOSE. *
16 * See the GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License along *
19 * with this program. If not, see <http://www.gnu.org/licenses/>. *
20 * *
21 * http://AudioProcessingFramework.github.com *
22 ******************************************************************************/
23
26
27#ifndef APF_THREADTOOLS_H
28#define APF_THREADTOOLS_H
29
30#include <thread>
31#include <chrono> // for std::this_thread::sleep_for()
32#include <mutex>
33#include <condition_variable>
34#include <atomic>
35
36#include "apf/misc.h" // for NonCopyable
37
38namespace apf
39{
40
41template<typename F>
42class ScopedThread : NonCopyable
43{
44 public:
45 ScopedThread(F f, int usleeptime)
46 : _keep_running(true)
47 , _function(f)
48 , _sleeptime(std::chrono::microseconds(usleeptime))
49 , _thread(std::thread(&ScopedThread::_thread_function, this))
50 {}
51
52 ~ScopedThread()
53 {
54 _keep_running.store(false, std::memory_order_release);
55 _thread.join();
56 }
57
58 private:
59 void _thread_function()
60 {
61 while (_keep_running.load(std::memory_order_acquire))
62 {
63 _function();
64 std::this_thread::sleep_for(_sleeptime);
65 }
66 }
67
68 std::atomic<bool> _keep_running;
69 F _function;
70 std::chrono::microseconds _sleeptime;
71 std::thread _thread;
72};
73
74class Semaphore : NonCopyable
75{
76 // implementation stolen from http://stackoverflow.com/a/19659736/500098
77
78 public:
79 Semaphore(int count = 0) : _count{count} {}
80
81 Semaphore(Semaphore&&)
82 {
83 // Semaphore must be movable in order to be stored in a std::vector.
84 // We never actually move it, so this should never be called:
85 throw std::logic_error("This is just a work-around, don't move!");
86 }
87
88 inline void post()
89 {
90 std::lock_guard<std::mutex> guard{_mtx};
91 _count++;
92 _cv.notify_one();
93 }
94
95 inline void wait()
96 {
97 std::unique_lock<std::mutex> lock{_mtx};
98 _cv.wait(lock, [this]() { return _count > 0; });
99 _count--;
100 }
101
102 private:
103 std::mutex _mtx;
104 std::condition_variable _cv;
105 int _count;
106};
107
108} // namespace apf
109
110#endif
Miscellaneous helper classes.
Audio Processing Framework.
Definition: iterator.h:61