I have code that spawns pthreadthat is trying to maintain a socket connection to a remote host. If the connection is ever lost, it tries to reconnect using a blocking call connect()on its socket. Since the code runs in a separate thread, I don't care that it uses a synchronous socket API.
That is, until the time comes for my application to exit. I would like to do some semblance of ordered shutdown, so I use thread synchronization primitives to wake up the stream and give a signal to exit, and then execute pthread_join()on the stream to wait for it to complete. This works fine if the thread is not in the middle of the call connect()when I command shutdown. In this case, I must wait for a timeout connection, which can be lengthy. This makes the application work for a long time.
What I would like to do is interrupt the call connect(). After the call returns, the stream will notice my output and turn off. Since it connect()is a system call, I thought I could intentionally interrupt it with a signal (thus returning the call EINTR), but I'm not sure if this is a reliable method in POSIX streams environments.
Does anyone have any recommendations on how to do this using signals or using some other method? As a side note, the call is connect()not available in some library code that I cannot change, so switching to a non-blocking socket is not an option.
source
share