与 Lite2D 项目中网络线程和主线程的交互方式类似,这里使用一个线程安全的队列作为两个线程之间的通信桥梁。

队列本身是线程安全的,内部通过互斥锁保护所有操作,并配合条件变量实现等待与唤醒机制。


                #include <iostream>
                #include <list>
                #include <string>
                #include <algorithm>
                #include <vector>
                #include <thread>
                #include <fstream>
                #include <stdlib.h>
                #include <stdio.h>
                #include "windows.h"
                #include <functional>
                #include "direct.h"
                #include <map>
                #include <memory>
                #include <queue>
                #include <condition_variable>
                #include <mutex>
                
                using namespace std;
                
                #define  LOCK(WHAT)   std::unique_lock<std::mutex> locker(WHAT);
                
                template<class T>
                class QueueThreadSafe
                {
                public:
                	void push(T &t)
                	{
                		LOCK(this->_mutex);
                		this->_queue.push(t);
                	}
                	T front()
                	{
                		LOCK(this->_mutex);
                		T & ret = this->_queue.front();
                		this->_queue.pop();
                		return ret;
                	}
                
                	bool empty()
                	{
                		LOCK(this->_mutex);
                		bool ret = this->_queue.empty();
                		return ret;
                	}
                private:
                	std::queue<T>  _queue;
                	std::mutex _mutex;
                
                };
                
                
                class UDPClient
                {
                public:
                	void send(char *buffer)
                	{
                
                		LOCK(this->_mutex);
                
                		reqs.push(buffer);
                
                		this->_cond.notify_all();
                	}
                	void loop()
                	{
                		while (this->isRun)
                		{
                			LOCK(this->_mutex);
                
                			if (this->reqs.empty() == true)
                			{//如果队列为空,那么等待唤醒
                				this->_cond.wait(this->_mutex);
                			}
                
                			cout << "send :" << reqs.front() << endl;
                		}
                	}
                
                	bool isRun = true;
                	std::mutex _mutex;
                	QueueThreadSafe<char*> reqs;
                	std::condition_variable_any _cond;
                };
                
                
                static  UDPClient* instance;
                
                static void workThreadFunc()
                {
                	instance = new UDPClient;
                	instance->loop();
                }
                
                
                int main()
                {
                
                	std::thread t(workThreadFunc);
                
                	t.detach();
                
                
                	char str[100];
                	int index = 0;
                
                	while (true)
                	{
                		//	Sleep(1);
                		sprintf(str, "%d", index++);
                		instance->send(str);
                	}
                
                	system("pause");
                
                	return 0;
                }