您的位置:首页 >> 编程开发 >> Visual C++ >> 进程和线程 >> 正文
进程和线程 RSS
 

具有Reset功能的多线程同步队列 - 2

http://www.rdxx.com 06年12月12日 20:21 Blog 我要投稿

关键词: 同步 , 功能 , 多线程 , 队列 , 线程

// ---------------------------------实现文件---------------------------------//

#include "threadsafequeue.h"

 

CTreadSafeMsgQueue::CTreadSafeMsgQueue(int QueSize, USHORT InvalidMsgType)

{

         INVALID_MSG_TYPE = InvalidMsgType;

        

         MAX_QUE_SIZE = QueSize;

         m_Queue = NULL;

        

         m_HeaderToWrite = 0;

         m_TailToRead = 0;

        

         m_S_Producer = NULL;

         m_S_Consumer = NULL;

         m_E_Queue = NULL;

 

    m_WritingThreadNum = 0;

         m_ReadingThreadNum = 0;

         m_bStop =  FALSE;

 

         m_bInitedOK = TRUE;   // 注意

 

         if (QueSize > 0) m_Queue = new MsgItem [QueSize];

         m_bInitedOK &= (m_Queue != NULL);

         if (!m_bInitedOK) return;

 

         m_S_Producer = CreateSemaphore(NULL, MAX_QUE_SIZE, MAX_QUE_SIZE, NULL);

         m_bInitedOK &= (m_S_Producer != NULL);

         if (!m_bInitedOK) return;

 

         m_S_Consumer = CreateSemaphore(NULL, 0, MAX_QUE_SIZE, NULL);

         m_bInitedOK &= (m_S_Consumer != NULL);

         if (!m_bInitedOK) return;

 

         m_E_Queue = CreateEvent(NULL, FALSE, TRUE, NULL);

         m_bInitedOK &= (m_E_Queue != NULL);

}

 

 CTreadSafeMsgQueue::~CTreadSafeMsgQueue()

{

         // 防止新的线程进入

         m_bInitedOK = FALSE;

 

         // 如果Reset,等待Reset完成

         while (m_bStop) Sleep(SLEEP_TIME);

 

         // 等待至少一类线程(读线程或写线程)退出同步状态

    while ((m_WritingThreadNum != 0) && (m_ReadingThreadNum != 0)) Sleep(SLEEP_TIME);

 

         // 此时线程必定阻塞于信号量(Semaphore)状态

        

9 7 3 1 2 3 4 8 :


 
 
标签: 同步 , 功能 , 多线程 , 队列 , 线程 打印本文
 
 
  热点搜索
 
 
 



Valid XHTML 1.0 Transitional
Copyright ©2005 - 2008 Rdxx.Com,All Rights Reserved
收藏本页
收藏本站