gpt4 book ai didi

c++ - 非阻塞 io_service::run

转载 作者:行者123 更新时间:2023-12-01 14:59:06 27 4
gpt4 key购买 nike

我正在尝试实现一个包含两个处理循环的 C++ 应用程序。当前,第一个处理循环(boost 的 io_service::run)阻止了第二个处理循环的执行。

利用线程或 std::async 方法的方法失败。 (我没有多线程的经验/背景)。

是否有一种优雅的方法可以在另一个线程中运行 io_service::run ,同时仍对传入的 UDP 数据报执行回调?

主文件:

class Foo
{
public:
Foo();

void callback(const int&);
private:
// ... (hopefully) non-relevant stuff...
};

int main()
{

Foo foo_obj;

// I need to run this function (blocking) but the constructor is blocking (io_server::run())
run();

return 0;
}


Foo::Foo(){
boost::asio::io_service io;

UDP_Server UDP_Server(io);

// Set function to be called on received message
UDP_Server.add_handler(std::bind(&Foo::callback, this, std::placeholders::_1));

// This function should be non-blocking
// -> tried several things, like threads, async, ... (unfortunately not successful)
io.run();
}

// realization of callback function here (see class definition)

包括自定义“库”:
class UDP_Server
{
public:
UDP_Server(boost::asio::io_service&);

void add_handler(std::function<void(int)>);

private:
// Function handle
std::function<void(int)> callbackFunctionHandle;

// Functions
void start_receive();
void handle_receive(const boost::system::error_code&, std::size_t);

// ... (hopefully) non-relevant stuff...
};

// Constructor
UDP_Server::UDP_Server(boost::asio::io_service& io_service)
: socket_(io_service, udp::endpoint(udp::v4(), UDP_PORT)){

}

// Store a callback function (class foo) to be called whenever a message is received
void UDP_Server::add_handler(std::function<void(int)> callbackFunction){
try
{
callbackFunctionHandle = callbackFunction;
start_receive();
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
}

// Async receive
UDP_Server::start_receive()
{
socket_.async_receive_from(
boost::asio::buffer(recv_buffer_), remote_endpoint_,
boost::bind(&UDP_Server::handle_receive, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}

// When message is received
void UDP_Server::handle_receive(const boost::system::error_code& error,
std::size_t bytes_transferred)
{
if (!error || error == boost::asio::error::message_size)
{

// ... do smth. with the received data ...

// Call specified function in Foo class
callbackFunctionHandle(some_integer);

start_receive();
}
else{
// ... handle errors
}

}

最佳答案

看看他们在 here 中做了什么:

boost::asio::io_service io_service;
/** your code here **/
boost::thread(boost::bind(&boost::asio::io_service::run, &io_service));
ros::spin();

因此,您基本上在与 ros::spin() 不同的线程中启动对 io_service::run() 的阻塞调用。

如果您启动绑定(bind)到单个 cpu 节点(为了不浪费 2 个 cpu 节点等待命令),您的调度程序可能会处理一些事情。

关于c++ - 非阻塞 io_service::run,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49436554/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com