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 ;}