-
Notifications
You must be signed in to change notification settings - Fork 0
/
PL_queue.cpp
91 lines (83 loc) · 1.73 KB
/
PL_queue.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "queue/blockingconcurrentqueue.h"
#include "include/Strategy.hpp"
#include <iostream>
#include <thread>
#include <memory>
#include <atomic>
#include <string>
#include <typeinfo>
using namespace std;
class Stra1
{
public:
void do_something() const
{
cout << "num 1 Strategy" << endl;
}
};
class Stra2
{
public:
void do_something() const
{
cout << "num 2 Strategy" << endl;
}
};
class Stra3
{
public:
void do_something() const
{
cout << "num 3 Strategy" << endl;
}
};
template<int I>
class Stra
{
public:
void do_something() const
{
cout << "num " << I << " Strategy" << endl;
}
};
using valueType = std::shared_ptr<Strategy>;
int main()
{
cout << std::thread::hardware_concurrency() << endl;
cout << std::this_thread::get_id() << endl;
std::shared_ptr<std::string> Str;
cout << atomic_is_lock_free(&Str) << endl;
moodycamel::BlockingConcurrentQueue<valueType> q;
std::thread producer([&]() {
int i;
cin >> i;
Stra1 s1;
Stra2 s2;
Stra3 s3;
valueType pi;
if (i == 1)
pi = std::make_shared<Strategy>(s1);
else if (i == 2)
pi = std::make_shared<Strategy>(s2);
else if (i == 3)
pi = std::make_shared<Strategy>(s3);
else {
cout << "error num" << endl;
return;
}
q.enqueue(pi);
cout << std::this_thread::get_id() << endl;
});
std::thread consumer([&]() {
valueType item;
q.wait_dequeue(item);
//cout << typeid(decltype(item)).name() << endl;
item->do_something();
cout << item->return_something() << endl;
//cout << *item << endl;
cout << std::this_thread::get_id() << endl;
});
producer.join();
consumer.join();
return 0;
}