-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlq.cpp
52 lines (47 loc) · 1.01 KB
/
lq.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
#include "p/base/queue.h"
#include <iostream>
#include <thread>
#include "p/base/logging.h"
constexpr int kFlag = 1000;
struct Node {
Node *next;
int thread_id = -1;
int value = -1;
};
void push(p::base::LinkedQueue<Node> &lq, int thread_id) {
int id = 0;
for (int i = 0; i < 1000; ++i) {
Node *p = new Node[kFlag];
for (int j = 1; j < kFlag; ++j) {
Node &tmp = p[j];
tmp.thread_id = thread_id;
tmp.value = ++id;
lq.push_back(&tmp);
}
Node &tmp = p[0];
p[0].thread_id = thread_id;
p[0].value = 0;
lq.push_back(&tmp);
}
}
void pop(p::base::LinkedQueue<Node> &lq) {
while (1) {
Node *ret = lq.pop_front();
if (ret) {
LOG_TRACE << ret->thread_id << "\t" << ret->value;
if (ret->value == 0) {
delete[] ret;
}
}
}
}
int main() {
p::base::LinkedQueue<Node> lq;
for (int i = 0; i < 16; ++i) {
std::thread tmp(push, std::ref(lq), i);
tmp.detach();
}
std::thread tmp(pop, std::ref(lq));
tmp.join();
return 0;
}