-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
84 lines (72 loc) · 2.32 KB
/
Copy pathmain.cpp
File metadata and controls
84 lines (72 loc) · 2.32 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <thread>
#include <vector>
#include <csignal>
#include <string>
#include <unistd.h>
#include <sstream>
#include <stdlib.h>
#include <time.h>
#include <atomic>
#include "blocking_queue.h"
// push队列操作线程数
const static int PUSH_THREAD_NUM = 5;
// pop队列操作线程数
const static int POP_THREAD_NUM = 50;
static std::atomic<bool> quit;
static void push_queue(int id, BlockingQueue* queue) {
std::string thread_name = "push_" + std::to_string(id);
while (!quit) {
// 产生0-9999随机数
int data = rand() % 10000;
size_t queue_size = queue->size();
queue->push(data);
// 构造完整字符串,防止由于std::cout在多线程下,线程不安全,完整字符串被分开
std::stringstream info;
info << "[" << thread_name << "] push queue, data=" << data << ", queue_size=" << queue_size << "\n";
std::cout << info.str();
usleep(200000);
}
}
static void pop_queue(int id, BlockingQueue* queue) {
std::string thread_name = "pop_" + std::to_string(id);
while (!quit) {
int data = queue->pop();
size_t queue_size = queue->size();
// 构造完整字符串,防止由于std::cout在多线程下,线程不安全,完整字符串被分开
std::stringstream info;
info << "[" << thread_name << "] pop queue, data=" << data << ", queue_size=" << queue_size << "\n";
std::cout << info.str();
usleep(200000);
}
}
// 捕捉信号,用于程序的优雅退出
static void signalHandler(int signum) {
quit = true;
}
int main(int argc, char** argv) {
quit = false;
signal(SIGINT, signalHandler);
signal(SIGTERM, signalHandler);
srand((unsigned int)time(NULL));
BlockingQueue queue;
std::vector<std::thread> threads;
for (int i = 0; i < PUSH_THREAD_NUM; ++i) {
threads.push_back(std::thread(push_queue, i, &queue));
}
for (int i = 0; i < POP_THREAD_NUM; ++i) {
threads.push_back(std::thread(pop_queue, i, &queue));
}
while (!quit) {
usleep(200000);
}
queue.quit();
for (std::thread& thread : threads) {
if (thread.joinable()) {
// 等待线程的自然结束
thread.join();
}
}
std::cout << "\nprogram going to quit\n";
return 0;
}