windows c线程等待队列数据推送
发布时间:2020-12-14 02:22:05 所属栏目:Windows 来源:网络整理
导读:我的程序设置如下: 有一个线程安全的队列类,一个线程在无限循环中将数据推送到它上面,另一个线程在坐在无限循环中时从中弹出数据.我试图想办法使用 Windows事件或其他机制来创建thread_1(下面),在无限循环中等待,并且只在队列深度大于或等于1时进行迭代. cl
我的程序设置如下:
有一个线程安全的队列类,一个线程在无限循环中将数据推送到它上面,另一个线程在坐在无限循环中时从中弹出数据.我试图想办法使用 Windows事件或其他机制来创建thread_1(下面),在无限循环中等待,并且只在队列深度大于或等于1时进行迭代. class thread-safe_Queue { public: push(); pop(); }; DWORD thread_1() { while(1) { // wait for thread-safe queue to have data on it // pop data off // process data } } DWORD thread_2() { while(1) { // when data becomes available,push data onto thread-safe queue } } 解决方法
我认为这可能会成功.派生类Event并重载Process()函数.
#include <process.h> // Along with all the normal windows includes //********************************************* using namespace os; Mutex globalQueueMutex; class QueueReader : public Event { public: virtual void Process() { // Lock the queue Locker l(globalQueueMutex); // pop data off // process data return; // queue will automatically unlock } }; QueueReader myQueueReader; //********************************************* // The queue writer would have functions like : void StartQueueReader() { Thread(QueueReader::StartEventHandler,&myQueueReader); } void WriteToQueue() { Locker l(globalQueueMutex); // write to the queue myQueueReader.SignalProcess(); // tell reader to wake up } // When want to shutdown void Shutdown() { myQueueReader.SignalShutdown(); } 以下是执行魔术的类. namespace os { // ********************************************************************** /// Windows implementation to spawn a thread. static uintptr_t Thread (void (*StartAddress)(void *),void *ArgList) { return _beginthread(StartAddress,ArgList); } // ********************************************************************** /// Windows implementation of a critical section. class Mutex { public: // Initialize section on construction Mutex() { InitializeCriticalSection( &cs_ ); } // Delete section on destruction ~Mutex() { DeleteCriticalSection( &cs_ ); } // Lock it void lock() { EnterCriticalSection( &cs_ ); } // Unlock it void unlock() { LeaveCriticalSection( &cs_ ); } private: CRITICAL_SECTION cs_; }; // class Mutex /// Locks/Unlocks a mutex class Locker { public: // Lock the mutex on construction Locker( Mutex& mutex ): mutex_( mutex ) { mutex_.lock(); } // Unlock on destruction ~Locker() { mutex_.unlock(); } private: Mutex& mutex_; }; // class Locker // ********************************************************************** // Windows implementation of event handler #define ProcessEvent hEvents[0] #define SetTimerEvent hEvents[1] #define ShutdownEvent hEvents[2] /// Windows implementation of events class Event { /// Flag set when shutdown is complete bool Shutdown; /// Max time to wait for events DWORD Timer; /// The three events - process,reset timer,and shutdown HANDLE hEvents[3]; public: /// Timeout is disabled by default and Events assigned Event( DWORD timer = INFINITE) : Timer(timer) { Shutdown = false; ProcessEvent = CreateEvent( NULL,TRUE,FALSE,NULL ); SetTimerEvent = CreateEvent( NULL,NULL ); ShutdownEvent = CreateEvent( NULL,NULL ); } /// Close the event handles virtual ~Event() { CloseHandle(ProcessEvent); CloseHandle(SetTimerEvent); CloseHandle(ShutdownEvent); } /// os::Thread calls this to start the Event handler static void StartEventHandler(void *pMyInstance) { ((Event *)pMyInstance)->EventHandler(); } /// Call here to Change/Reset the timeout timer void ResetTimer(DWORD timer) { Timer = timer; SetEvent(SetTimerEvent); } /// Set the signal to shutdown the worker thread processing events void SignalShutdown() { SetEvent(ShutdownEvent); while (!Shutdown) Sleep(30);} /// Set the signal to run the process void SignalProcess() { SetEvent(ProcessEvent); } protected: /// Overload in derived class to process events with worker thread virtual void Process(){} /// Override to process timeout- return true to terminate thread virtual bool Timeout(){ return true;} /// Monitor thread events void EventHandler() { DWORD WaitEvents; while (!Shutdown) { // Wait here,looking to be signaled what to do next WaitEvents = WaitForMultipleObjects(3,hEvents,Timer); switch (WaitEvents) { // Process event - process event then reset for the next one case WAIT_OBJECT_0 + 0: Process(); ResetEvent(ProcessEvent); break; // Change timer event - see ResetTimer(DWORD timer) case WAIT_OBJECT_0 + 1: ResetEvent(SetTimerEvent); continue; // Shutdown requested so exit this thread case WAIT_OBJECT_0 + 2: Shutdown = true; break; // Timed out waiting for an event case WAIT_TIMEOUT: Shutdown = Timeout(); break; // Failed - should never happen case WAIT_FAILED: break; default: break; } } } }; } // namespace os (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Windows10 配置JAVA环境变量方法
- windows-vista – 连接到NAT后面的支持Teredo的服务器
- Windows Phone 7 – SkyDrive的编程访问
- Windows – 如何将名称设置为Win32线程?
- NTFS 16 TB的卷大小限制是“真实的”吗?
- 如何升级Windows Netbeans的Subversion版本
- Windows Azure中角色的静态IP地址?
- windows-server-2008 – Windows 2008忽略了Gratuitous ARP
- 授予用户在Windows Server 2012域控制器上更改系统时间的权
- 从Windows窗体迁移到WPF …值得吗?
推荐文章
站长推荐
- windows – 在Visual Basic 6中访问动态加载的DL
- winapi – 使用Windows API检测所有连接的显示器
- wpf – 将TextBlock绑定到Window的属性
- windows-server-2008-r2 – 401 – 在Server 200
- windows – 除非收到消息,否则有没有办法入睡?
- windows-7 – 错误0x80070570:如何删除损坏和不
- windows-7 – “Windows Virtual PC”和“Micros
- windows-server-2003 – PHP MYSQL站点性能
- windows-server-2012-r2 – WSUS和推出Win 10 17
- windows-server-2008 – 如何将Windows Server 2
热点阅读