forked from AlexanderBabansky/rtaudio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadSuspendable.h
More file actions
39 lines (34 loc) · 881 Bytes
/
ThreadSuspendable.h
File metadata and controls
39 lines (34 loc) · 881 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#pragma once
#include <condition_variable>
#include <functional>
#include <mutex>
#ifdef WIN32
#include <thread>
#else
#include <pthread.h>
#endif
class ThreadSuspendable
{
public:
ThreadSuspendable(std::function<bool()> process, bool realtime = false, int priority = 0);
ThreadSuspendable(const ThreadSuspendable &) = delete;
ThreadSuspendable &operator=(const ThreadSuspendable &) = delete;
~ThreadSuspendable();
void resume();
void suspend();
void stop();
bool isValid() const;
//do not call this
void threadMethod();
private:
enum class State { SUSPENDED, RUNNING, STOPPED, RESUMING, SUSPENDING, STOPPING };
State mState = State::SUSPENDED;
std::function<bool()> mProcess = nullptr;
#ifdef WIN32
std::thread mThread;
#else
pthread_t mThread = 0;
#endif
std::mutex mMutex;
std::condition_variable mCV;
};