【问题标题】:Boost Asio UDP + Threading = get_id() error at thread.hppBoost Asio UDP + Threading = get_id() thread.hpp 错误
【发布时间】:2018-07-19 21:19:58
【问题描述】:

我正在尝试将一个简单的 UDP 阅读器与一个线程连接,以便在创建 UDP 阅读器类时,它会初始化一个从套接字读取内容并处理数据的线程。该代码在 IDE 中没有显示任何问题,但是当我尝试编译它时,出现以下错误:

Error   C2064   term does not evaluate to a function taking 0 arguments (thread.hpp, line 116)

当我查看更多详细信息时,我的 IDE 显示 thread.hpp 的 get_id() 函数存在问题:

Error (active)      declaration is incompatible with "boost::thread::id boost::this_thread::get_id()" (thread.hpp, line 665).

我不知道该怎么做,我没想到会看到头文件中的错误。请帮忙。

这是我的课程代码:

class networkUDPClient {
public:
    const char * hostAddress;
    unsigned int port;

    void (*customFunction)(void ** args);
    void ** argumentPointers;

    utilityTripleBuffer * buffer;

    boost::asio::io_service service;
    boost::asio::ip::udp::socket socket;
    boost::asio::ip::udp::endpoint listenerEndpoint;
    boost::asio::ip::udp::endpoint senderEndpoint;

    boost::thread_group * threads = new boost::thread_group();

    boost::thread * readThread;
    boost::thread::id readThreadID;

    // Function Definitions
    void readCallback(const boost::system::error_code & error, std::size_t read_bytes) {
        this->customFunction(this->argumentPointers);
    };

    void readSocket(void) {
        while (1) {
            this->socket.async_receive_from(
                boost::asio::buffer(
                    this->buffer->currentBufferAddr,
                    this->buffer->bufferSize),
                senderEndpoint,
                0,
                boost::bind(
                    &networkUDPClient::readCallback,
                    this,
                    boost::asio::placeholders::error,
                    boost::asio::placeholders::bytes_transferred));
        }
    }

    void startReadProcess(void) {
        this->service.run();

        this->threads->create_thread(&networkUDPClient::readSocket);

        this->readThreadID = this->readThread->get_id();
    }

    void stopReadProcess(void) {
        this->threads->join_all();
            this->threads->~thread_group();

        this->socket.close();

        this->service.stop();
    }

    // Constructors
    networkUDPClient(const char * hostAddress, unsigned int port, void (*customFunction)(void ** args), void ** argumentPointers) :
        socket(service),
        listenerEndpoint(boost::asio::ip::address_v4::from_string(hostAddress), port)
    {
        this->buffer = (utilityTripleBuffer*)argumentPointers[0];

        this->customFunction = customFunction;
        this->argumentPointers = argumentPointers;

        this->hostAddress = hostAddress;
        this->port = port;

        socket.open(this->listenerEndpoint.protocol());
        socket.set_option(boost::asio::ip::udp::socket::reuse_address(true));
        socket.bind(this->listenerEndpoint);

        this->startReadProcess();
    };
    ~networkUDPClient()
    {
        this->stopReadProcess();
    };
};

也欢迎对我为 UDP 目的实现线程的任何批评。

【问题讨论】:

  • 这是 fullcomplete 错误消息吗?没有其他东西可以返回代码中的一行?
  • 哦,您还有其他一些问题。例如readThread 指向哪里?尽量避免使用指针。
  • 错误消息准确地指出了它提到的位置、文件和行(出于隐私原因,我删除了计算机目录的路径)。 readThread 变量未使用,我一直在考虑这样做(创建一个线程,然后将其添加到线程组)。一旦我解决了这个问题,我可能会在以后使用它。感谢您的关注。
  • 您的构造函数调用startReadProcess,后者调用readThread->get_id()。所以不,它没有被使用。
  • 很抱歉,您遇到的问题和上一个问题stackoverflow.com/q/48673558/8918119 一样。在this->threads->create_thread(&networkUDPClient::readSocket); 行上,您不能传递非静态成员函数,使用bind() 或传递this 作为参数。看看你的get_id() 问题是否再次出现。

标签: c++ boost boost-asio


【解决方案1】:

您不能将非静态成员函数传递给 boost::thread_group::create_thread() 使用 bind() 或 lambda 并查看您的 get_id() 问题是否再次出现。

this->threads->create_thread(boost::bind(&networkUDPClient::readSocket, this));

Demo

create_thread() 文档说:

创建一个新的boost::thread 对象,就像new thread(threadfunc) 一样,并将其添加到组中。

但这是 boost::thread 类的单参数构造函数,仅采用 thread(F f); 一个 Callable,而不是绑定参数的 variadic template

template <class F,class A1,class A2,...>
    thread(F f,A1 a1,A2 a2,...);

【讨论】:

  • 就像我在评论中发布的那样,thread() 函数会自动绑定它需要的任何函数。我尝试了类似您之前发布的内容,但没有成功。我也用了你的代码,也没用。来源:stackoverflow.com/questions/13742648/…
  • @AdmiralFishHead,我在没有 _1 的情况下更新了我的答案 boost::bind(&amp;networkUDPClient::readSocket, this)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-07
  • 2014-09-02
  • 1970-01-01
  • 2013-12-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多