-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
65 lines (63 loc) · 1.38 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<iostream>
#include<thread>
#include<vector>
#include<string>
#include<list>
#include <mutex>
class base{
private:
std::list<int> ls;
std::mutex my_mutex;
int n{100};
public:
void get_request(){
for(int i = 0; i < n; ++i){
std::lock_guard<std::mutex> manager_lock(my_mutex);
ls.push_back(i);
std::cout << "get_request " << i << '\n';
}
}
void process_request(){
// int res = -1;
int res = -1;
for(int i = 0; i < n; ++i){
bool tmp = get_data(res);
if(tmp){
std::cout << "process_request " << res << '\n';
} else{
std::cout << "request empty()\n";
}
}
}
bool get_data(int &res){
std::lock_guard<std::mutex> manager_lock(my_mutex);
if(!ls.empty()){
res = ls.front();
ls.pop_front();
return true;
} else {
return false;
}
}
};
class tmp{
private:
int x;
public:
tmp(int xx): x(xx){}
class B{
public:
void f(const tmp &other) const{
std::cout << other.x << std::endl;
}
};
};
// int tmp::x = 10;
int main(){
base A;
std::thread write_obj(&base::process_request, &A);
std::thread read_obj(&base::get_request, &A);
read_obj.join();
write_obj.join();
return 0;
}