-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblacknote.cpp
More file actions
67 lines (57 loc) · 1.62 KB
/
blacknote.cpp
File metadata and controls
67 lines (57 loc) · 1.62 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
#include <chrono>
#include <thread>
#include <algorithm>
#include <iostream>
#include <mutex>
#include <vector>
#include <windows.h>
#include <memory>
std::string names[] = {"Alice", "Bob", "John", "Sam", "Anna"};
std::mutex m;
class Employee {
public:
std::string name;
Employee(std::string _name)
{
name = _name;
}
};
void call_employee(Employee* e1, Employee* e2) {
m.lock();
std::cout << e1->name << " берёт трубку и набирает абоненту " << e2->name << std::endl;
std::cout << e1 << " адреса " << e2 << std::endl;
Sleep(10);
std::cout << "Разговор..." << std::this_thread::get_id() << std::endl;
Sleep(1000);
m.unlock();
Sleep(10);
}
std::unique_ptr<Employee> initialize(Employee& emp) {
std::unique_ptr<Employee> ptr = std::make_unique<Employee> (emp);
std::cout<<ptr.get()<<" указатель в init ---- "<<ptr.get()->name<<std::endl;
return ptr;
}
int main() {
SetConsoleOutputCP(CP_UTF8);
std::vector<Employee> emps;
std::vector<std::thread> threads;
srand(time(0));
for (int i = 0; i < 5; ++i) {
emps.emplace_back(Employee (names[i]));
//std::cout<<emps[i].name<<" ";
}
for (int i = 0; i < 2; i++) {
int a = rand() % 5;
int b = rand() % 5;
while (b == a) {
b = rand() % 5;
}
Employee * p1 = initialize(emps[a]).get();
Employee * p2 = initialize(emps[b]).get();
threads.emplace_back(call_employee, p1, p2);
}
for (auto& thread : threads) {
thread.join();
}
return 0;
}