⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 logqueue.h

📁 短信开发源码
💻 H
字号:
/*********************************************************************************************
**********************************************************************************************
****
****		File name:						LOGQUEUE.H
****		Creation	date(DD.MM.YY):		
****		Author:							
****
****		Comment:
****
****		Revisions:
****		$Log$
****		Edit by tomfan 2003/07/03
****		Revision 1.0	2003/03/107	Tom fan
****		1.Add member fucntion "bool IsEmpty()"
****
****
***********************************************************************************************
***********************(C)	Copyright Giesecke & Devrient	2003******************************/
#ifndef LOGQUEUE_H
#define LOGQUEUE_H

class LOGQueue{
    public:
       //----------------
       // Queue
       //----------------
	  LOGQueue(UINT limit)
	  {
	   handles[SemaphoreIndex] = ::CreateSemaphore(NULL,  // no security attributes
	                                               0,     // initial count
	                                               limit, // max count
					               NULL); // anonymous

	   handles[StopperIndex] = ::CreateEvent(NULL,        // no security attributes
				                 TRUE,        // manual reset
				                 FALSE,       // initially non-signaled
				                 NULL);       // anonymous
	   
	   ::InitializeCriticalSection(&lock);
	  } // Queue

       //----------------
       // ~Queue
       //----------------
       ~LOGQueue()
	  {
	   ::CloseHandle(handles[SemaphoreIndex]);
	   ::CloseHandle(handles[StopperIndex]);
	   ::DeleteCriticalSection(&lock);
	  } // ~Queue

       //----------------
       // AddTail
       //----------------
       BOOL AddTail(CString p)
	  { 
	   BOOL result;
	   ::EnterCriticalSection(&lock);
	   queue.AddTail(p);
	   result = ::ReleaseSemaphore(handles[SemaphoreIndex], 1, NULL);
	   if(!result)
	      { /* failed */
	       // caller can use ::GetLastError to determine what went wrong
	       queue.RemoveTail();
	      } /* failed */
	   ::LeaveCriticalSection(&lock);
	   return result;
	  } // AddTail

       //----------------
       // RemoveHead
       //----------------
       CString RemoveHead()
	  { 
	   CString result = _T("");
	   
	   switch(::WaitForMultipleObjects(2, handles, FALSE, INFINITE))
	      { /* decode */
	       case StopperIndex:   // shut down thread
		  ::ExitThread(10);  // kill thread
		  return result;      // return keeps C compiler happy

	       case SemaphoreIndex: // semaphore
		  ::EnterCriticalSection(&lock);
		  result = queue.RemoveHead();
		  ::LeaveCriticalSection(&lock);
		  return result;

	       case WAIT_TIMEOUT: // not implemented
	       default:
		  ASSERT(FALSE); // impossible condition
		  return result;
	      } /* decode */
	  } // RemoveHead

       //----------------
       // shutdown
       //----------------
       void shutdown()
	  {
	   ::SetEvent(handles[StopperIndex]);
	  } // shutdown

	   bool IsEmpty()
	   {
		   if (queue.IsEmpty())
			   return true;

		   return false;
	   }

    protected:
       enum {StopperIndex, SemaphoreIndex};
       HANDLE handles[2];
       CRITICAL_SECTION lock;
       CList<CString, CString&> queue;
};
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -