码迷,mamicode.com
首页 > 编程语言 > 详细

C++ 队列的实现

时间:2016-06-25 22:59:57      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:

 1 /************************************************************************/
 2 /* 实现一个通用同步队列
 3    使用链表实现队列 (先入先出)
 4    使用信号量等待实现同步
 5 /************************************************************************/
 6 #pragma once
 7 
 8 #include "lock.h"
 9 #include <list>
10 
11 template <typename Type>
12 class Queue
13 {
14 public:
15     inline Queue() 
16     {
17         bInit = true;
18         count = 0;
19         semaphore.Create(0xFFFF);
20     }
21     inline ~Queue() 
22     {
23         Close();
24     }
25 
26     inline void Close()
27     {
28         if(bInit) {
29             bInit = false;
30             semaphore.Release(1);            //释放信号 让正在wait的就返回
31             semaphore.Close();
32         }
33     }
34 
35     inline int getCount()
36     {
37         return count;
38     }
39 
40     inline bool Push(Type* type)
41     {
42         if(!bInit)
43             return false;
44 
45         criticalsection.Lock();
46 
47         queueList.push_back(type);            
48         count++;
49 
50         criticalsection.Unlock();
51 
52         semaphore.Release(1);                //发出信号 让Pop可以操作了
53         return true;
54     }
55     inline Type* Pop()
56     {
57         semaphore.Wait(INFINITE);            //等待Push的信号发来后才进行动作
58         if(bInit == false)
59             return 0;
60 
61         criticalsection.Lock();
62 
63         Type *type = *(queueList.begin());    //取出链表头
64         queueList.pop_front();                //从链表弹出
65         count--;
66 
67         criticalsection.Unlock();
68         return type;
69     }
70 
71 
72 private:
73     bool bInit;
74     int     count;
75     Semaphore    semaphore;
76     CriticalSection criticalsection;
77     std::list<Type*> queueList;
78 };

 

 

其中用到了简单包装的Semaphore和CriticalSection

如下:

 

 

 1 //临界区
 2 class CriticalSection
 3 {
 4 public:
 5     inline CriticalSection()
 6     {
 7         InitializeCriticalSection(&m_sect);
 8     }
 9     inline ~CriticalSection()
10     {
11         DeleteCriticalSection(&m_sect);
12     }
13     inline void Lock()
14     {
15         EnterCriticalSection(&m_sect);
16     }
17     inline void Unlock()
18     {
19         LeaveCriticalSection(&m_sect);
20     }
21     inline BOOL TryLock()
22     {
23         return TryEnterCriticalSection(&m_sect);
24     }
25 
26 private:
27     CRITICAL_SECTION m_sect;
28 };
29 
30 
31 
32 //信号量
33 class Semaphore
34 {
35 public:
36     inline Semaphore()
37     {
38         m_hSem = NULL;
39     }
40     inline ~Semaphore()
41     {
42         Close();
43     }
44     inline BOOL Create(LONG lMaximumCount)
45     {
46         m_hSem = CreateSemaphore(NULL, 0, lMaximumCount, NULL);
47         return (m_hSem != NULL);
48     }
49     inline void Close()
50     {
51         if (m_hSem)
52         {
53             CloseHandle(m_hSem);
54             m_hSem = NULL;
55         }
56     }
57     inline BOOL Release(LONG lReleaseCount)
58     {
59         return ReleaseSemaphore(m_hSem, lReleaseCount, NULL);
60     }
61     inline DWORD Wait(DWORD dwMilliseconds)
62     {
63         return WaitForSingleObject(m_hSem, dwMilliseconds);
64     }
65 
66 public:
67     HANDLE  m_hSem;
68 };

 

C++ 队列的实现

标签:

原文地址:http://www.cnblogs.com/codetask/p/5617036.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!