forked from cryfs/cryfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadSystem.h
More file actions
47 lines (36 loc) · 1.51 KB
/
ThreadSystem.h
File metadata and controls
47 lines (36 loc) · 1.51 KB
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
40
41
42
43
44
45
46
47
#pragma once
#ifndef MESSMER_CPPUTILS_THREAD_THREADSYSTEM_H
#define MESSMER_CPPUTILS_THREAD_THREADSYSTEM_H
#include "../macros.h"
#include <boost/thread.hpp>
#include <list>
#include <functional>
namespace cpputils {
//TODO Test
class ThreadSystem final {
private:
struct RunningThread {
std::string threadName;
std::function<bool()> loopIteration; // The loopIteration callback returns true, if more iterations should be run, and false, if the thread should be terminated.
boost::thread thread; // boost::thread because we need it to be interruptible.
};
public:
using Handle = std::list<RunningThread>::iterator;
static ThreadSystem &singleton();
Handle start(std::function<bool()> loopIteration, std::string threadName);
void stop(Handle handle);
private:
ThreadSystem();
static void _runThread(std::function<bool()> loopIteration);
static void _onBeforeFork();
static void _onAfterFork();
//TODO Rename to _doOnBeforeFork and _doAfterFork or similar, because they also handle locking _mutex for fork().
void _stopAllThreadsForRestart();
void _restartAllThreads();
boost::thread _startThread(std::function<bool()> loopIteration, const std::string& threadName);
std::list<RunningThread> _runningThreads; // std::list, because we give out iterators as handles
boost::mutex _mutex;
DISALLOW_COPY_AND_ASSIGN(ThreadSystem);
};
}
#endif