Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

breaking out from socket select

Tags:

c++

c

sockets

I have a loop which basically calls this every few seconds (after the timeout):

 while(true){

    if(finished)
       return;

    switch(select(FD_SETSIZE, &readfds, 0, 0, &tv)){
        case SOCKET_ERROR : report bad stuff etc; return;
        default : break;
    }

    // do stuff with the incoming connection
 }

So basically for every few seconds (which is specified by tv), it reactivates the listening.

This is run on thread B (not a main thread). There are times when I want to end this acceptor loop immediately from thread A (main thread), but seems like I have to wait until the time interval finishes..

Is there a way to disrupt the select function from another thread so thread B can quit instantly?

like image 729
kamziro Avatar asked Feb 04 '23 05:02

kamziro


1 Answers

The easiest way is probably to use pipe(2) to create a pipe and add the read end to readfds. When the other thread wants to interrupt the select() just write a byte to it, then consume it afterward.

like image 162
Ignacio Vazquez-Abrams Avatar answered Feb 06 '23 16:02

Ignacio Vazquez-Abrams