下面我们以一个简单的队列应用。来说明
Code
1#include <string>
2#include <list>
3
4//第一步:定义你要用到的结构体
5typedef struct _QUERY_DATA //队列的数据
6{
7 char data1[3];
8 char data2[2];
9}TQueryData,*PQueryData;
10
11
12//第二步
13typedef std::list<TQueryData *> QueryMyList;
14
15CRITICAL_SECTION m_rCriticalSection; //声明临界资源
16
17class QueryMyQueue : public QueryMyList
18{
19
20 public:
21 QueryReqQueue()
22 {
23 ::InitializeCriticalSection(&m_rCriticalSection); //初始化一个临界资源对象
24 }
25 virtual ~QueryReqQueue()
26 {
27 ::DeleteCriticalSection(&m_rCriticalSection); //删除一个临界资源对象
28 }
29
30 bool AddTail(TQueryReq *data) //添加到队列的尾部
31 {
32 ::EnterCriticalSection(&m_rCriticalSection);
33 push_back(data);
34 ::LeaveCriticalSection(&m_rCriticalSection);
35
36 return true;
37 }
38
39 int GetCount() //获得队列的个数
40 {
41 int nCount;
42 ::EnterCriticalSection(&m_rCriticalSection);
43 if (! empty())
44 {
45 nCount = size();
46 }
47 else
48 {
49 nCount = 0;
50 }
51
52 ::LeaveCriticalSection(&m_rCriticalSection);
53 return nCount;
54 }
55
56 TQueryReq * GetFront() //移动到队列的头部
57 {
58 TQueryReq *data;
59 ::EnterCriticalSection(&m_rCriticalSection);
60 if (! empty())
61 {
62 data = front();
63 pop_front();
64 }
65 else
66 {
67 data = NULL;
68 }
69 ::LeaveCriticalSection(&m_rCriticalSection);
70 return data;
71 }
72
73
74 void Clear() //清除
75 {
76 ::EnterCriticalSection(&m_rCriticalSection);
77 while (! empty())
78 {
79 TQueryReq *data = front();
80 pop_front();
81 delete data;
82 }
83 ::LeaveCriticalSection(&m_rCriticalSection);
84 }
85
86 public:
87 CRITICAL_SECTION m_rCriticalSection;
88};
89
90//如何在代码中进行应用
91QueryMyQueue m_MyReqQueue;
92
93 if ( pUserQuery->m_MyReqQueue.GetCount() > 500 ) //记得要包含Query的头文件
1#include <string>
2#include <list>
3
4//第一步:定义你要用到的结构体
5typedef struct _QUERY_DATA //队列的数据
6{
7 char data1[3];
8 char data2[2];
9}TQueryData,*PQueryData;
10
11
12//第二步
13typedef std::list<TQueryData *> QueryMyList;
14
15CRITICAL_SECTION m_rCriticalSection; //声明临界资源
16
17class QueryMyQueue : public QueryMyList
18{
19
20 public:
21 QueryReqQueue()
22 {
23 ::InitializeCriticalSection(&m_rCriticalSection); //初始化一个临界资源对象
24 }
25 virtual ~QueryReqQueue()
26 {
27 ::DeleteCriticalSection(&m_rCriticalSection); //删除一个临界资源对象
28 }
29
30 bool AddTail(TQueryReq *data) //添加到队列的尾部
31 {
32 ::EnterCriticalSection(&m_rCriticalSection);
33 push_back(data);
34 ::LeaveCriticalSection(&m_rCriticalSection);
35
36 return true;
37 }
38
39 int GetCount() //获得队列的个数
40 {
41 int nCount;
42 ::EnterCriticalSection(&m_rCriticalSection);
43 if (! empty())
44 {
45 nCount = size();
46 }
47 else
48 {
49 nCount = 0;
50 }
51
52 ::LeaveCriticalSection(&m_rCriticalSection);
53 return nCount;
54 }
55
56 TQueryReq * GetFront() //移动到队列的头部
57 {
58 TQueryReq *data;
59 ::EnterCriticalSection(&m_rCriticalSection);
60 if (! empty())
61 {
62 data = front();
63 pop_front();
64 }
65 else
66 {
67 data = NULL;
68 }
69 ::LeaveCriticalSection(&m_rCriticalSection);
70 return data;
71 }
72
73
74 void Clear() //清除
75 {
76 ::EnterCriticalSection(&m_rCriticalSection);
77 while (! empty())
78 {
79 TQueryReq *data = front();
80 pop_front();
81 delete data;
82 }
83 ::LeaveCriticalSection(&m_rCriticalSection);
84 }
85
86 public:
87 CRITICAL_SECTION m_rCriticalSection;
88};
89
90//如何在代码中进行应用
91QueryMyQueue m_MyReqQueue;
92
93 if ( pUserQuery->m_MyReqQueue.GetCount() > 500 ) //记得要包含Query的头文件