Sign in to follow this  

Unblocking the accept function

This topic is 3597 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working on this piece of code that lies on a thread, blocking socket-based. My current problem that needs solving is that the application crashes when we exit it. Reason? Well, the application starts to exit, but as I have discovered, one thread, the one with the blocking accept function, doesn't self-terminate, and so it causes the shutdown code of the application to crash because some memory seems to be getting released and used at the same time... I already have an inter-thread event system in place to deal with these types of system-wide messaging, but in this particular case the thread is "dead", awaiting a socket connection. Is there anyway I can "legally" unblock the accept function, so that the thread can unblock, read it event messages, realize the system is shutting down, and release its allocate mem blocks?

Share this post


Link to post
Share on other sites
There's two options:
1. As Dave() suggests, use select() to make the socket non-blocking, or only select() for 100ms or so at a time, so you can poll the shutdown signal
2. Create another socket that you connect() to the accept()ing socket to signal shutdown. Then, at shutdown you can connect() to 127.0.0.1 on whatever port you're using, which will wake up the accept()ing thread, and it can check if a shutdown is in progress, and if so it can ignore the new connection and quit.

#1 is probably the simplest, so long as you don't mind making the accepting thread a bit more poll-like.

Share this post


Link to post
Share on other sites
Thank you both for your replies, I'll have to research this more before I actually write some code, but at least I now see some light at the end of the tunnel.

R++.

Share this post


Link to post
Share on other sites

This topic is 3597 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this