用STL设计消息队列、优先级消息队列、资源分配管理器

          STL库老早已经成为C++的一部分,在使用C++开发项目的过程中,很多人还在犹豫要不要使用STL库,觉得STL库很难,其实不然。我工作的项目中现在大量使用STL库,STL使用调试简单,高效,可以减少很多重复的代码。

       本文的主要目的是使用STL的queue 和 priority queue来阐述下项目中经常使用的消息队列以及资源分配模式。本文的例子主要如下:

  • 消息队列
  • 带优先级的消息队列
  • 资源分配管理器

    STL容器

    我们将使用下面的容器来实现本文的例子:

     

    queue 队列容器支持添加一个元素,并且从中删除一个元素,也可以变成一个双端队列
    priority_queue 向队尾添加一个新元素,如果该优先权大于前面的元素,将放在前面,元素的优先权由用户指定的函数传入。
    stack 有后进先出的特点,可以选择选择vetctor或其他线性列表放入容器。

     

        

     

    1.消息队列

        消息队列是项目中经常需要使用的模式,下面使用STL的queue容器实现:

      

    #include // STL header file for queue #include using namespace std; // Specify that we are using the std namespace class Message; class Message_Queue { typedef queue > MsgQueType; MsgQueType m_msgQueue; public: void Add(Message *pMsg) { // Insert the element at the end of the queue m_msgQueue.push(pMsg); } Message *Remove() { Message *pMsg = NULL; // Check if the message queue is not empty if (!m_msgQueue.empty()) { // Queue is not empty so get a pointer to the // first message in the queue pMsg = m_msgQueue.front(); // Now remove the pointer from the message queue m_msgQueue.pop(); } return pMsg; } int GetLength() const { return m_msgQueue.size(); } };

     

    2.优先级消息队列

          上面的消息队列类只支持在队列的尾部添加一个元素,在许多的应用程序中,需要根据消息的优先级将消息添加到队列中,当一个高优先级的消息来了,需要将该消息添加到所有比它优先级低的消息前面,下面将使用priority_queue来实现优先级消息队列类。

        

    函数对象(Functors)

    优先级消息队列的实现和上面消息队列实现相似,唯一不同的是这里使用了函数对象来决定优先权CompareMessages,该结构重载了操作符"(,)",该机制可以实现可以将一个函数作为一个参数,和函数指针对比有如下好处:

           1.函数对象消息更高,可以是内联函数,函数指针总是有函数调用的开销。

            2.函数对象提供了一个安全的实现方法,这样的实现不会出现空指针访问。

    #include // STL header file for queue #include using namespace std; // Specify that we are using the std namespace class Message; class Priority_Message_Queue { struct Entry { Message *pMsg; int priority; }; struct Compare_Messages { bool operator () (const Entry& left , const Entry& right) { return (left.priority < right.priority); } }; typedef priority_queue, Compare_Messages > Message_Queue_Type; Message_Queue_Type m_message_Queue; public: void Add(Message *pMsg, int priority) { // Make an entry Entry entry; entry.pMsg = pMsg; entry.priority = priority; // Insert the element according to its priority m_message_Queue.push(entry); } Message *Remove() { Message *pMsg = NULL; // Check if the message queue is not empty if (!m_message_Queue.empty()) { // Queue is not empty so get a pointer to the // first message in the queue pMsg = (m_message_Queue.top()).pMsg; // Now remove the pointer from the message queue m_message_Queue.pop(); } return (pMsg); } size_t Get_Length() const { return m_message_Queue.size(); } };

         

    3.资源分配管理器

    这里使用queue 和 stack两个容器来实现一个简单的资源分配器,用该容器来保存空闲的资源列表。

    该资源分配器支持如下接口:

           1.Construction:当这个资源分配器构造出来,需要给定空闲的资源列表,这些资源将添加到这个空闲资源列表。

           2.Allocate:当需要一个资源时,需要从空闲资源队列中移除一个资源,并返回给调用者。

          3.Free:当一个资源被释放,需要将该资源加入空闲资源列表。

          4.GetFreeResourceCount:返回当前可用的资源数目。

    Coldest First(使用queue)

    #include // STL header file for queue #include using namespace std; // Specify that we are using the std namespace class Resource; class Cold_Resource_Allocator { typedef queue > Free_Queue_Type; Free_Queue_Type m_free_Resource_Queue; public: Cold_Resource_Allocator(int resource_Count, Resource *resource_Array[]) { for (int i = 0; i < resource_Count; i++) { m_free_Resource_Queue.push(resource_Array[i]); } } Resource * Allocate() { Resource *pResource = NULL; // Check if any free resources are available. if (!m_free_Resource_Queue.empty()) { // Queue is not empty so get a pointer to the // first resource in the queue pResource = m_free_Resource_Queue.front(); // Now remove the pointer from the free resource queue m_free_Resource_Queue.pop(); } return pResource; } void Free(Resource *pResource) { // Insert the resource at the end of the free queue m_free_Resource_Queue.push(pResource); } size_t GetFreeResourceCount() { return m_free_Resource_Queue.size(); } };

     

    Hottest First(使用statck)

    #include // STL header file for stack #include using namespace std; // Specify that we are using the std namespace class Resource; class Hot_Resource_Allocator { typedef stack > Free_Stack_Type; Free_Stack_Type m_free_Resource_Stack; public: Hot_Resource_Allocator(int resource_Count, Resource *resource_Array[]) { for (int i = 0; i < resource_Count; i++) { m_free_Resource_Stack.push(resource_Array[i]); } } Resource * Allocate() { Resource *pResource = NULL; // Check if any free resources are available. if (!m_free_Resource_Stack.empty()) { // Queue is not empty so get a pointer to the // first resource in the stack pResource = m_free_Resource_Stack.top(); // Now remove the pointer from the free resource stack m_free_Resource_Stack.pop(); } return pResource; } void Free(Resource *pResource) { // Insert the resource at the end of the free stack m_free_Resource_Stack.push(pResource); } size_t GetFreeResourceCount() { return m_free_Resource_Stack.size(); } };

     

     

     

波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » 用STL设计消息队列、优先级消息队列、资源分配管理器

发表评论

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡