-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSemaphore_01.cpp
More file actions
62 lines (42 loc) · 1.5 KB
/
Semaphore_01.cpp
File metadata and controls
62 lines (42 loc) · 1.5 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
// ===========================================================================
// Semaphore // Semaphore_01.cpp
// ===========================================================================
#include "../Logger/Logger.h"
#include <chrono>
#include <iostream>
#include <print>
#include <semaphore>
#include <thread>
namespace ConcurrencyBinarySemaphore {
class Worker
{
private:
std::binary_semaphore m_semaphore;
public:
Worker() : m_semaphore{ 0 } {}
void scheduleJob()
{
Logger::log(std::cout, "ScheduleJob: Data preparing ...");
std::this_thread::sleep_for(std::chrono::seconds{ 5 });
Logger::log(std::cout, "ScheduleJob: Data prepared!");
m_semaphore.release();
}
void executeJob()
{
Logger::log(std::cout, "ExecuteJob: Waiting for data ...");
m_semaphore.acquire();
Logger::log(std::cout, "ExecuteJob: Executing job ...");
std::this_thread::sleep_for(std::chrono::seconds{ 3 });
Logger::log(std::cout, "ExecuteJob: Done.");
}
};
}
void test_binary_semaphore_01() {
using namespace ConcurrencyBinarySemaphore;
Worker worker{};
std::jthread t1{ &Worker::scheduleJob, &worker };
std::jthread t2{ &Worker::executeJob, &worker };
}
// ===========================================================================
// End-of-File
// ===========================================================================