使用boost::asio::io_服务::post()

使用boost::asio::io_服务::post(),boost,boost-asio,boost-thread,boost-bind,Boost,Boost Asio,Boost Thread,Boost Bind,首先我问这个 所以现在我试着这样做: 下面是控制台C++项目,我完全模拟了我的大项目 TestServicePost.cpp #include "stdafx.h" #include "SomeClass.h" int _tmain(int argc, _TCHAR* argv[]) { SomeClass* s = new SomeClass(); while(true) { s->update(); } return 0; }

首先我问这个

所以现在我试着这样做:

下面是控制台C++项目,我完全模拟了我的大项目

TestServicePost.cpp

#include "stdafx.h"
#include "SomeClass.h"


int _tmain(int argc, _TCHAR* argv[])
{
    SomeClass* s = new SomeClass();
    while(true)
    {
        s->update();
    }
    return 0;
}
某个班级

#include <boost/thread.hpp>
#include <boost/asio.hpp>
#include <queue>

class ServiceNote
{
public:
    std::string getType()
    {
        std::stringstream typeSS;
        typeSS << "LamasaTech.MultiWall.PostNote." << (NoteType.compare("Normal") == 0 ? "Node" : "Header") << "." << Shape << "." << Colour;
        return typeSS.str();
    }
    int Action; 
    int CNoteId;    
    std::string Colour; 
    int NoteId; 
    std::string NoteType;   
    int SessionId;  
    std::string Shape;  
    std::string Style;  
    std::string Text;   
    int X;  
    int Y;  
};

class SomeClass
{
public:
    SomeClass();
    ~SomeClass();
    void update();

private:
    std::queue<ServiceNote> pendingNotes;
    void addToQueue(ServiceNote sn);
    void pollService(boost::asio::io_service* svc);
    int getMessage(boost::asio::io_service* svc, std::string sessionId, int messageId);
    boost::thread servicePoller;
};
我无法将io_服务定义为类成员,因此我可以在析构函数
~SomeClass()
中使用它,如果您能提供帮助,我将不胜感激


如果io_service.post对我来说不是最好的解决方案,请推荐一些东西,正如你所看到的,我有一个构造函数、析构函数和一个更新方法,每次都会调用它,我尝试单独使用这个和队列,但它不是线程安全的,是否有易于使用的线程安全FIFO?

SomeClass
构造函数中,您实际上可以执行以下操作:

SomeClass::SomeClass()
{
    boost::shared_ptr< boost::asio::io_service > io_service(
        new boost::asio::io_service
    );
    io_servicePoller = io_service;
    servicePoller = boost::thread(boost::bind(&SomeClass::pollService, this, io_servicePoller));
}
  • 定义本地
    io\u服务
    实例
    
  • 调用其
    run()
    成员函数,因为io\U服务没有工作
  • 将本地对象的地址传递给另一个线程
  • 这肯定行不通


    请注意,
    io\u service::run()
    是一种“消息循环”,因此它应该阻止调用线程。不要在对象构造函数中调用它。

    我想出了如何将io_服务声明为类成员:

    boost::shared_ptr< boost::asio::io_service > io_servicePoller;
    
    在更新中,我调用了run,它将内容添加到队列中,然后在while循环中读取它们

    void SomeClass::update()
    {   
        io_servicePoller->run();
        io_servicePoller->reset();
        while(!pendingNotes.empty())
        {
            ServiceNote sn = pendingNotes.front();
    
            pendingNotes.pop();
        }
    }
    
    并将我的成员签名更改为
    void SomeClass::pollService(boost::shared\u ptrsvc)

    因此发生的情况是:

  • 应用程序启动
  • 这是我的课
  • 我的类创建一个服务并启动线程
  • 线程从服务获取项目
  • 主线程检查io服务队列并将其导出
  • 然后它使用队列
  • 多亏了伊戈尔,没有他我是做不到的


    还有,我在哪里得到了如何创建共享指针的方法呢?

    谢谢,我修复了它,现在我将发布我的解决方案:)@Shereef创建另一个成员函数,给
    io_服务
    一些
    work
    ,并调用
    run
    作为最后一个语句(它将阻止):
    void run(){io_service io_servicePoller;io_service::work work work work(io_servicePoller);servicePoller=thread(bind(&SomeClass::pollService,this,&io_servicePoller));io_servicePoller.run()}
    我不明白你写的是什么,但我发布了我实现的解决方案,但效果不好。如果你坚持这样使用
    io_service::run()
    (即,要仅处理io_服务队列中当前可用的项目并退出),您必须在它之前调用
    io_servicePoller->reset()
    ,否则
    run
    将仅第一次工作。但我在io_servicePoller->run()之后添加了io_servicePoller->reset()无论如何,因为我尊重你,我相信你比我更有经验
    SomeClass::SomeClass()
    {
        boost::shared_ptr< boost::asio::io_service > io_service(
            new boost::asio::io_service
        );
        io_servicePoller = io_service;
        servicePoller = boost::thread(boost::bind(&SomeClass::pollService, this, io_servicePoller));
    }
    
    SomeClass::~SomeClass()
    {
        servicePoller.interrupt();
        io_servicePoller->stop();
        servicePoller.join();
    }
    
    void SomeClass::update()
    {   
        io_servicePoller->run();
        io_servicePoller->reset();
        while(!pendingNotes.empty())
        {
            ServiceNote sn = pendingNotes.front();
    
            pendingNotes.pop();
        }
    }