Domanda

The problem is simple,

I have a daemon thread which waits for incoming client connections and when atleast one client connects, it exits.

Now, when someone calls shutdownApp function, I need to send the signal(or interrupt) to the daemon thread and ask it to come out of blocking accept so that it can exit.

I don't want to use

1) Selects (or non blocking threads)
2) TerminateThread

MFC mentions that a winsock's accept function can be interrupted via Asynchronous Procedure Calls. If anyone has pointers on how to do it, it would be great.

È stato utile?

Soluzione

Simply close the socket that accept() is being called on. That will cause accept() to fail with an error code that the thread can then check for. If you read the documentation more carefully, it mentions that an APC can abort accept() prematurely to warn you against calling accept() again while the APC is still running. That does not mean you should intentionally use an APC to abort accept(), that is the wrong solution.

If you do not want to close the socket, then use select() in a loop. It works on both blocking and non-blocking sockets, and will tell you when to call accept() so it does not block. Specify a timeout so that your thread can wake up periodically to look for a termination condition before calling select() again.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top