在thread.hpp

时间:2018-02-09 06:45:13

标签: c++ boost boost-asio

我正在尝试将简单的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目的线程的任何批评也是受欢迎的。

1 个答案:

答案 0 :(得分:0)

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

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

Demo

create_thread()文档说:

  

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

但这是boost::thread类的单个参数构造函数,thread(F f);Callable,而variadic template也绑定了参数:

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