【问题标题】:Boost.Bind'ing a member function and posting it to io_serviceBoost.Bind 一个成员函数并将其发布到 io_service
【发布时间】:2015-08-29 05:22:26
【问题描述】:

我正在尝试包装一个表示要由 io_service 完成的工作的对象。

作业是任意类型的,不一定是 IO 操作。类似于here所描述的。

我已经能够发布绑定的常规函数,但无法发布成员函数

为什么这段代码不能编译:

#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 的指南,但它不起作用。

【问题讨论】:

  • 编译错误是什么?

标签: c++ c++11 boost boost-asio boost-bind


【解决方案1】:

问题是most vexing parse 的结果。特别是,以下声明了两个函数:

class_fun1 f1(); // function declaration
class_fun2 f2(); // function declaration

第一个声明了一个名为f1 的函数,它不接受任何参数并返回一个class_func1 的实例。它没有声明标识符为f1class_func1 实例。 f2 也有类似的情况。

要解决此问题,请删除括号,将代码更改为:

class_fun1 f1; // declares a variable
class_fun2 f2; // declares a variable

鉴于 clang 的编译器输出消息,有时最好打开编译器警告并尝试使用它进行编译。特别是在尝试用clang解析原始代码时,它提供了一些helpful output

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 何时阻止和解除阻止的更多详细信息,请考虑阅读this 问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-20
    • 1970-01-01
    相关资源
    最近更新 更多