-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_deque.cpp
executable file
·74 lines (60 loc) · 1.73 KB
/
test_deque.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
/*
* Copyright (C) 2016, 王超 [wabcwang] , <[email protected]>.
* All Rights Reserved.
*/
#include <iostream> // std::cout
#include <thread> // std::thread
#include <chrono> // std::chrono::seconds
#include <mutex> // std::mutex, std::unique_lock
#include <condition_variable> // std::condition_variable, std::cv_status
#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include <unistd.h>
#include "threaddeque.h"
//入队列参数结构
struct SendMsgStruct
{
int sCmd; //命令类型
int sValue; //可以是SQL,一般为字符串内容
};
ThreadDeque<SendMsgStruct*> m_vMsgQue;
bool g_bisok = false;
void read_value() {
g_bisok=true;
std::cout<<"read thread started\r\n";
while( true == m_vMsgQue.wait_for_second(2)){
SendMsgStruct *tmp = NULL;
while(NULL != (tmp= m_vMsgQue.pop_front())){
std::cout<<"read--cmd:"<<tmp->sCmd<<"\r\n";
delete tmp;
}
//int j = (rand() % 10) + 1;
//usleep(j*100000);
//std::cout<<j<<"\r\n";
}
return;
}
int main ()
{
std::thread th (read_value);
while(true != g_bisok)
{
std::cout<<"wait for thread started\r\n";
usleep(100);
}
for(int i=0;i <10; i++)
{
int j = (rand() % 10)+1;
SendMsgStruct *tmp=new SendMsgStruct;
tmp->sCmd= i;
tmp->sValue = j;
usleep(j*100000);
std::cout<<"write--cmd:"<<tmp->sCmd<<"\r\n";
m_vMsgQue.push_back(tmp);
}
sleep(1);
m_vMsgQue.notify_exit();
th.join();
return 0;
}