Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to exit node with Ctrl+C, if I/O read() is in blocking stage?

I am trying to read CANBUS on a parallel thread. I initialized canbus in main thread because I want to make sure that CAN cables are connected. By initializing, I mean setsockopt(), ioctl(), bind() to configure the socket.

void readCanbus(int soktId) {
        int nbytes = read(soktId, ...);

int main() {
    int soktId;
    someSocketSetupFn(soktId);    // it takes argument as ref

    std::thread t(readCanbus, soktId);
    while (ros::ok)
        // something to do

Problem: If there is no incoming CAN messages, read() is blocked. Ctrl+C doesn't terminate the C++11 Program.

How can I make the read() terminate and so the whole program?

Terminate thread c++11 blocked on read. This post proposed a solution for POSIX. I am working on ubuntu16.04.