-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
50 lines (46 loc) · 1.39 KB
/
test.cpp
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
#include "stlxx.h"
#include <algorithm>
#include <iostream>
#include <sstream>
#include <thread>
#include <vector>
struct Employee {
std::string id;
Employee(Employee &&) = default;
Employee(std::string id) : id(id) {}
std::vector<std::string> lunch_partners;
};
int main(int argc, char *argv[])
{
Employee mat("mat");
std::thread *workers[16];
stlxx::atomic<std::vector<std::int64_t>> vec;
stlxx::atomic<Employee> mel = Employee{"mel"}, bob = Employee{"bob"};
for (auto &worker : workers) {
worker = new std::thread([=, &mat]() {
std::stringstream s;
s << std::this_thread::get_id();
std::int64_t i64; s >> i64;
vec->push_back(i64);
using mutex = std::recursive_mutex&;
stlxx::synchronized([=, &mat]() {
mel->lunch_partners.push_back(mat.id);
mat.lunch_partners.push_back(bob->id);
bob->lunch_partners.push_back(mel->id);
}, mutex(mel), mutex(bob));
});
}
for (auto &worker : workers) {
worker->join();
delete worker;
}
auto &v = vec;
auto &v1 = mel->lunch_partners;
auto &v2 = mat.lunch_partners;
auto &v3 = bob->lunch_partners;
std::cout << v->size() << ": "
<< v1.size() << ": "
<< v2.size() << ": "
<< v3.size() << '\n';
return 0;
}