-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEventLoop.h
More file actions
98 lines (78 loc) · 2.93 KB
/
Copy pathEventLoop.h
File metadata and controls
98 lines (78 loc) · 2.93 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// ===========================================================================
// EventLoop.h
// ===========================================================================
#pragma once
#include "../Logger/Logger.h"
#include <condition_variable>
#include <functional>
#include <future>
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>
class EventLoop
{
private:
using Event = std::move_only_function<void()>;
private:
std::vector<Event> m_events;
std::mutex m_mutex;
std::condition_variable m_condition;
std::jthread m_thread;
bool m_running;
public:
// c'tor(s) / d'tor
EventLoop();
~EventLoop();
// prevent copy semantics
EventLoop(const EventLoop&) = delete;
EventLoop& operator= (const EventLoop&) = delete;
// prevent move semantics
EventLoop(EventLoop&&) noexcept = delete;
EventLoop& operator= (EventLoop&&) noexcept = delete;
// public interface
void enqueue(Event callable); // handles both existing Event objects and raw lambdas/callables
// convenience method to bind arguments automatically
template<typename TFunc, typename ... TArgs>
void enqueueTask(TFunc&& func, TArgs&& ...args)
{
Logger::log(std::cout, "enqueueTask ...");
// using "Generalized Lambda Capture" to preserve move semantics
auto callable {
[func = std::forward<TFunc>(func),
... capturedArgs = std::forward<TArgs>(args)] () mutable -> void {
std::invoke(std::move(func), std::move(capturedArgs) ...); // more flexibel
// std::move(func) (std::move(capturedArgs) ...); // less flexibel
// func(std::move(capturedArgs) ...); // less flexibel
}
};
{
// RAII guard
std::lock_guard<std::mutex> guard{ m_mutex };
m_events.push_back(std::move(callable));
}
m_condition.notify_one();
}
// same method, avoiding code duplication of the mutex locking and notification logic,
// leaving enqueue() as the single place responsible for inserting events into the queue.
template<typename TFunc, typename ... TArgs>
void enqueueTaskEx(TFunc&& func, TArgs&& ...args)
{
Logger::log(std::cout, "enqueueTaskEx ...");
auto callable{
[func = std::forward<TFunc>(func),
... capturedArgs = std::forward<TArgs>(args)]() mutable -> void {
std::invoke(std::move(func), std::move(capturedArgs) ...);
}
};
// using the central, thread-safe enqueue method.
enqueue(std::move(callable));
}
void start();
void stop();
private:
void threadProcedure();
};
// ===========================================================================
// End-of-File
// ===========================================================================