珠海网站建设设计,网站首页不见怎么做,比较好的设计网站有哪些,福州网上店铺搭建公司主线程和子线程进行list通信#xff0c;要用到互斥锁#xff0c;避免同时操作 1、封装线程基类XThread控制线程启动和停止#xff1b; 2、模拟消息服务器线程#xff0c;接收字符串消息#xff0c;并模拟处理#xff1b; 3、通过Unique_lock和mutex互斥方位list 消息队列…主线程和子线程进行list通信要用到互斥锁避免同时操作 1、封装线程基类XThread控制线程启动和停止 2、模拟消息服务器线程接收字符串消息并模拟处理 3、通过Unique_lock和mutex互斥方位list 消息队列 4、主线程定时发送消息给子线程
代码包含了XThread类基类、XMsgSever类消息、测试主程序
//XThread.h 类基类
#pragma once
#include thread//基类
class XThread
{
public:virtual void Start();//启动线程virtual void Stop();//设置线程退出标志并等待virtual void Wait();//等待virtual bool is_exit();//线程是否退出private:virtual void Main() 0;//线程入口纯虚函数子函数必须单独实现bool is_exit_ false;std::thread th_;};
//XThread.cpp 类基类
#include XThread.husing namespace std;//在CPP中引用using namespace
void XThread::Start()//启动线程
{is_exit_ false;//不要退出th_ thread(XThread::Main,this);}
void XThread::Stop()//设置线程退出标志并等待
{is_exit_ true;Wait();}
void XThread::Wait()//等待
{if (th_.joinable()){th_.join();}}
bool XThread::is_exit()//线程是否退出
{return is_exit_;
}//XMsgSever.h 类
#pragma once
#include XThread.h
#include list
#include mutex
class XMsgSever:public XThread
{
public://给当前线程发消息void SendMsg(std::string msg);private:void Main()override;//重写main,override检查名字是否写错std::liststd::string msgs_;//消息队列缓冲std::mutex mux_;//互斥访问消息队列
};
//XMsgSever.cpp 类
#include XMsgSever.h
#include iostreamusing namespace std;void XMsgSever::SendMsg(std::string msg)
{//消息生产者unique_lockmutex lock(mux_);//为了保证list的线程安全加锁msgs_.push_back(msg);
}void XMsgSever::Main()
{//消息消费者while (!is_exit()){this_thread::sleep_for(10ms);unique_lockmutex lock(mux_);if (msgs_.empty()){//如果没有消息则continuecontinue;}while (!msgs_.empty()){//消息处理业务逻辑cout recv: msgs_.front().c_str() endl;msgs_.pop_front();}}}
//测试主程序
/*1、封装线程基类XThread控制线程启动和停止
2、模拟消息服务器线程接收字符串消息并模拟处理
3、通过Unique_lock和mutex互斥方位liststring 消息队列
4、主线程定时发送消息给子线程*/#include XMsgSever.h
#include sstream
using namespace std;int main()
{XMsgSever server;server.Start();//启动子线程调了Main入口进入死循环消费信息了for (int i 0; i 10; i){stringstream ss;ss msg: i 1;server.SendMsg(ss.str());this_thread::sleep_for(500ms);}server.Stop();printf(All done!\n);return 0;
}运行结果