在thread.hpp上boost asio udp 螺纹= get_id()错误



我正在尝试将简单的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目的实施线程的任何批评。

您不能将非静态成员函数传递给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 <class F,class A1,class A2,...>
    thread(F f,A1 a1,A2 a2,...);

最新更新