Boost控制多线程使用教程
来源:互联网 发布:幸村精市 知乎 编辑:程序博客网 时间:2024/06/08 18:46
boost UDP网络发送多线程
头文件一般而言放在 public.hpp 中
#include <boost/thread/thread.hpp> #include <boost/thread/thread_time.hpp> #include <iostream> #include <string> #include <boost/asio.hpp> #include <boost/bind.hpp>using namespace boost ;using namespace std ;
如果使用QT 在工程文件中加入路径如下:
INCLUDEPATH += /home/zchx/Qt_Project/NET_Watcher/Swaper/
unix|win32: LIBS += -lboost_thread
unix|win32: LIBS += -lboost_system
main文件中的多线程开关:
thread UDP_thread_Send(&main_UDP_Send);//初始化线程 UDP_thread_Receive.interrupt();//结束UDP_thread_Receive.start_thread(); //开始
注意:在头文件中允许其他CPP文件调用的extern 写法 extern thread UDP_thread_Send ;
在boost中start_thread是一个私有的函数,需要修改thread.hpp对它进行允许
代码示例如下:
UDP的网络发送改写,可控制起断的多线程控制
#include "public.hpp"char wat[500];class sender{public: sender(boost::asio::io_service& io_service, const boost::asio::ip::address& multicast_address,int multicast_port) : endpoint_(multicast_address, multicast_port), socket_(io_service, endpoint_.protocol()), timer_(io_service), StrLength_(0) { this_thread::interruption_point(); timer_.expires_from_now(boost::posix_time::millisec(100)); timer_.async_wait( boost::bind(&sender::handle_timeout, this, boost::asio::placeholders::error)); if(statebuffer.DirSendList.size()<=0) { statebuffer.BackMSGtoUI = FileDirNotFound ; if(statebuffer.UDP_Send_Started==1) { UDP_thread_Send.interrupt(); this_thread::sleep(posix_time::milliseconds(200)); } } else{ statebuffer.BackMSGtoUI = FileDirFound ; this_thread::sleep(posix_time::milliseconds(100)); unsigned int *LengthAdd = & StrLength_ ; char *str = Load_File_AtLength(statebuffer.DirSendList.front().c_str(),4096,LengthAdd); memset(Msg,0,2048); memcpy(Msg,str,StrLength_); free(str); this_thread::interruption_point(); socket_.async_send_to( boost::asio::buffer(Msg), endpoint_, boost::bind(&sender::handle_send_to, this, boost::asio::placeholders::error)); } } void handle_send_to(const boost::system::error_code& error) { this_thread::interruption_point(); if (!error) { statebuffer.DirSendList.pop_front(); if(statebuffer.DirSendList.size()>0) { //char *str ; SendRound(); } else{ this_thread::sleep(boost::posix_time::seconds(1)); statebuffer.DirSendList.assign(statebuffer.DirSendListBack.begin(),statebuffer.DirSendListBack.end()); SendRound(); } } } void SendRound() { unsigned int *LengthAdd = &StrLength_ ; char *str = Load_File_AtLength(statebuffer.DirSendList.front().c_str(),4096,LengthAdd); memset(Msg,0,2048); memcpy(Msg,str,StrLength_); free(str); //Swaper.DirSendList.pop_front(); this_thread::interruption_point(); socket_.async_send_to( boost::asio::buffer(Msg), endpoint_, boost::bind(&sender::handle_send_to, this, boost::asio::placeholders::error)); } void handle_timeout(const boost::system::error_code& error) { this_thread::interruption_point(); if (!error) { timer_.expires_from_now(boost::posix_time::millisec(100)); timer_.async_wait( boost::bind(&sender::handle_timeout, this, boost::asio::placeholders::error)); } }private: boost::asio::ip::udp::endpoint endpoint_; boost::asio::ip::udp::socket socket_; boost::asio::deadline_timer timer_; unsigned int StrLength_ ; char Msg[2048] ;};int main_UDP_Send(){ try { boost::asio::io_service io_service; this_thread::sleep(posix_time::milliseconds(100)); stringstream portbuf ; portbuf<<statebuffer.UDP_port ; int UDPPORT ; portbuf>>UDPPORT ; this_thread::interruption_point(); sender s(io_service, boost::asio::ip::address::from_string(statebuffer.UDP_Addr),UDPPORT); this_thread::interruption_point(); io_service.run(); this_thread::interruption_point(); } catch (std::exception& e) { std::cerr << "Exception: " << e.what() << "\n"; } /********************************************************* * Interrupt Point **********************************************************/ catch(thread_interrupted&) { statebuffer.UDP_Send_Started = 0 ; printf("thread interrupted\n"); } return 0 ;}
- Boost控制多线程使用教程
- boost 多线程使用
- boost----多线程使用
- <Boost> boost::thread 多线程的使用
- boost多线程使用简例
- VS2005使用boost库教程
- boost::shared_ptr的多线程使用陷阱
- Boost.Aiso教程 5-同步多线程程序中的处理程序
- boost 多线程
- boost多线程
- Boost多线程
- boost 多线程
- boost多线程
- BOOST多线程
- boost 多线程
- 控制台中使用多线程编程
- android 使用多线程控制进度条
- boost教程
- IDL 数组 做下标问题
- Unity3D 2D动画插件EasyMotion2D (之五)Sprite Splitter
- 日语单词
- 孩子的解决问题能力
- JS操作JSON总结
- Boost控制多线程使用教程
- rtx消息 虚拟机xp 通知到 主机ubuntu
- Oracle 10g 一些基本的查询语句(一)(
- Android处理图片OOM的若干方法小结
- cookie的域和路径
- Unity3D 2D动画插件EasyMotion2D (之六)SpriteRenderer
- Unity3D 2D动画插件EasyMotion2D (之七)Atlas Editor
- 你为什么会浮躁
- 关于dict和v$fixed_table的使用(转)