I am trying to connect a simple UDP reader with a thread, so that when UDP reader class is created, it initializes a thread that reads stuff from the socket and processes the data. The code does not show any issues in the IDE, but when I attempt to compile it, I get the following Errors:
Error C2064 term does not evaluate to a function taking 0 arguments (thread.hpp, line 116)
When I check it for more details, my IDE shows me an issue with the thread.hpp's get_id() function:
Error (active) declaration is incompatible with "boost::thread::id boost::this_thread::get_id()" (thread.hpp, line 665).
I am not sure what to do with this actually, one thing I did not expect to see is errors in the header files. Please help.
This is my class code:
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();
};
};
Any criticism on my implementation of the threading for UDP purposes is also welcome.