VC语言

本类阅读TOP10

·VC++ 学习笔记(二)
·用Visual C++打造IE浏览器(1)
·每个开发人员现在应该下载的十种必备工具
·教你用VC6做QQ对对碰外挂程序
·Netmsg 局域网聊天程序
·Windows消息大全
·VC++下使用ADO编写数据库程序
·VC++学习笔记(四)
·非法探取密码的原理及其防范
·怎样在VC++中访问、修改注册表

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
具有Reset功能的多线程同步队列 - 3

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

BOOL CTreadSafeMsgQueue::GetMsg(MsgItem &Msg, int WaitTime)

{

         if (!m_bInitedOK) return FALSE;

 

         // 防止在reset期间导致Semaphore计数不正确

         while (m_bStop)

         {

                  TRACE("Thread %d Sleep\n", GetCurrentThreadId());

                  Sleep(SLEEP_TIME);

         }

 

         // 进入同步操作状态

         m_ReadingThreadNum++;

 

         if (WaitForSingleObject(m_S_Consumer, WaitTime) == WAIT_OBJECT_0)

         {

                   if (WaitForSingleObject(m_E_Queue, WaitTime) == WAIT_OBJECT_0)

                   {

                            // OK now, Get message then

                           Msg = m_Queue[m_TailToRead++];

                            if (m_TailToRead >= MAX_QUE_SIZE) m_TailToRead = 0;

                            TRACE("Get message === %d\n", Msg.MsgID);

                           

                            ReleaseSemaphore(m_S_Producer, 1, NULL);

                            SetEvent(m_E_Queue);

 

                            m_ReadingThreadNum--;

                            return TRUE;

                   }

                   else // wait event time out

                   {

                            // not Get message so release Consumer

                            ReleaseSemaphore(m_S_Consumer, 1, NULL);

                            SetEvent(m_E_Queue);

        

                            m_ReadingThreadNum--;

                            return FALSE;

                   }

         }

         else // wait semaphore time out

         {

                  m_ReadingThreadNum--;

                   return FALSE;

         }

}




相关文章

相关软件