-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpseudo_notif.cpp
More file actions
37 lines (35 loc) · 805 Bytes
/
pseudo_notif.cpp
File metadata and controls
37 lines (35 loc) · 805 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
#include <iostream>
#include <string>
#include <thread>
int main(int argc, char* argv[])
{
size_t nb = 1;
if (argc >= 2)
{
nb = std::stoul(argv[1]);
}
for (size_t i = 0; i < nb; i++)
{
bool done = false;
std::string payload;
std::thread t1([&done, &payload] {
// 1: waiting for the payload to be ready
// read of done
while (!done) // #1
{
}
// 3: use payload
std::cout << payload << "\n";
});
std::thread t2([&done, &payload] {
payload = "Really very big string that we don't see the end, even now we ";
payload += "don't know when it'll stop...";
// 2: the payload is ready:
// write of done at "the same time" as #1
done = true;
});
t1.join();
t2.join();
}
return 0;
}