Boost.绑定成员函数并将其发布到io_service
Boost.Bind'ing a member function and posting it to io_service
我正在尝试包装一个代表io_service完成的工作的对象。
作业是任意类型的,不必是IO操作。与这里描述的类似。
我已经能够发布绑定常规函数,但是不能发布成员函数
为什么这段代码不能编译:
#include <iostream>
#include "boost/asio.hpp"
#include "boost/thread.hpp"
using namespace std;
namespace asio = boost::asio;
class class_fun1 {
public:
void an_expensive_calculation(int num) {
cout << "an_expensive_calculation: " << num << endl;
}
};
class class_fun2 {
public:
void a_long_running_task(int num) {
for (int x = 0; x < num; ++x)
cout << "a_long_running_task: " << num << endl;
}
};
int main(int argc, char** argv) {
int my_thread_count = 4;
asio::io_service io_service;
asio::io_service::work work(io_service);
boost::thread_group threads;
for (std::size_t i = 0; i < my_thread_count; ++i)
threads.create_thread(boost::bind(&asio::io_service::run, &io_service));
class_fun1 f1();
class_fun2 f2();
io_service.post(boost::bind(&class_fun1::an_expensive_calculation, &f1, 42));
io_service.post(boost::bind(&class_fun2::a_long_running_task, &f2, 123));
threads.join_all();
return 0;
}
#include <iostream>
#include "boost/asio.hpp"
#include "boost/thread.hpp"
using namespace std;
namespace asio = boost::asio;
void an_expensive_calculation(int num) {
cout << "an_expensive_calculation: " << num << endl;
}
void a_long_running_task(int num) {
for (int x = 0; x < num; ++x)
cout << "a_long_running_task: " << num << endl;
}
int main(int argc, char** argv) {
int my_thread_count = 4;
asio::io_service io_service;
asio::io_service::work work(io_service);
boost::thread_group threads;
for (std::size_t i = 0; i < my_thread_count; ++i)
threads.create_thread(boost::bind(&asio::io_service::run, &io_service));
io_service.post(boost::bind(an_expensive_calculation, 42));
io_service.post(boost::bind(a_long_running_task, 123));
threads.join_all();
return 0;
}
我浏览了一些在线教程和文档,据我所知,第一个应该是可行的。我遵循了绑定成员函数并将其发布到io_service的指导方针,但它没有工作。
这个问题是最令人烦恼的解析结果。特别是,下面声明了两个函数:
class_fun1 f1(); // function declaration
class_fun2 f2(); // function declaration
第一个声明了一个名为f1
的函数,该函数不接受参数并返回class_func1
的一个实例。它不声明一个标识符为f1
的class_func1
实例。类似的情况也适用于f2
。
要解决这个问题,请删除括号,将代码更改为:
class_fun1 f1; // declares a variable
class_fun2 f2; // declares a variable
给定clang的编译器输出消息,有时最好打开编译器警告并尝试使用它进行编译。特别是,当尝试用clang解析原始代码时,它提供了一些有用的输出:
main.cpp:35:18: error: empty parentheses interpreted as a function declaration [-Werror,-Wvexing-parse]
class_fun1 f1();
^~
main.cpp:35:18: note: remove parentheses to declare a variable
class_fun1 f1();
同样,由于boost::asio::work
对象的生命周期,程序将永远不会终止,因为线程组将永远不会成功加入。要解决这个问题,可以考虑在加入线程组之前销毁工作对象,或者在运行工作之前将工作提交到io_service
。关于io_service
何时阻塞和解除阻塞的更多细节,请考虑阅读这个问题。
相关文章:
- Seg Fault Issue C++ (file IO / getline)
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 为什么在读取文件大小时文件IO速度会发生变化
- Agora.io 虚幻引擎插件构建错误
- 不将数据 socket.io c++(客户端)发送到 nodejs(服务器)socket.io
- ASIO signal_set多个 IO 线程不可靠,具体取决于代码顺序?
- 通过 Tor 服务C++ socket.io 客户端
- 如何使用可视化代码和平台IO将环境变量注入CPP文件?
- 如何读取 google::p rotobuf::io::CodedOutputStream::WriteVarint32
- Conan.io 在编译步骤中或已经在签出时
- C++中真正的异步文件 IO
- 在C++中返回 IO 对象的目的是什么?
- 使用 Broadcast 发出的从节点服务器发送的数据不能被 C++ 套接字 IO 客户端读取
- C++网络 IO、文件处理和网络故障
- Xcode OSX上的C++构建失败,出现多个错误文件IO..不可用:在macOS 10.15中引入
- Android Studio-在现有的旧项目中启用本机C++调试(card.io Android Source)
- 如何检测函数是否执行IO操作
- 如何键入用于const对象的自定义io操纵器
- libevent是否允许在不同的线程中运行timer/io的回调
- IO服务重新启动后,Boost最后期限计时器持续触发