-
Notifications
You must be signed in to change notification settings - Fork 0
/
Workers.h
48 lines (34 loc) · 832 Bytes
/
Workers.h
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
48
//
// Created by henri on 29.01.2024.
//
#ifndef EVENT_LOOP_THREADING_WORKERS_H
#define EVENT_LOOP_THREADING_WORKERS_H
#include <functional>
#include <iostream>
#include <list>
#include <mutex>
#include <thread>
#include <vector>
#include <queue>
#include <condition_variable>
#include "chrono"
class Workers {
public:
explicit Workers(int num)
: num_threads(num)
{}
void start();
void post( const std::function<void()>& task);
void post_timeout(const std::function<void()>& task, int delay);
void join();
private:
std::vector<std::thread> threads;
std::list<std::function<void()>> tasks;
int num_threads;
std::mutex tasks_mutex;
std::condition_variable cv;
bool stop_threads = false;
bool stop();
void work();
};
#endif //EVENT_LOOP_THREADING_WORKERS_H