Re: Closing an accepting BSD socket from a different thread

Stephan Gatzka

Hello Paul!

Thanks for the answer.

Paradigmatically correct approach to this situation is:
1. Avoid sharing I/O resources (not just sockets) across different
2. If/when you can't avoid it, you need to synchronize access to those
resources from different threads using synchronization primitives
(mutexes, semaphores, etc.)
Sure, no doubt on that. The problem is, that I need a mechanism to "unblock" the accept.
Yes, I could use non-blocking sockets with poll(), but I also found no easy to use mechanism to "unblock" poll(). I can't just send a signal to that thread which called poll() like it would work in Linux.

Eventually, we'll need to catch and fix such cases. But the only
visible effect for well-behaving applications following the guidelines
above will be bloating code size in the Zephyr network stack/socket
implementation (so hopefully, we won't get bad community stereotypes
due to that). If you have a small reproduction testcase for the issue,
definitely please submit it at
Will do.

My question is how I can safely "unblock" the thread waiting in the
A way to not block forever in accept() call is to use timed poll() on
that socket. The thread issuing the poll() call would be the best
party to know when to close this socket (e.g., if there's no
activity during some period of time). Other threads could signal the
owner thread that they want something to be done to the socket via flag
variables. E.g., following is a well-know pattern:
=== main loop thread ===
while (!should_exit) {
poll(..., MAIN_LOOP_PERIOD);
=== other threads ===
should_exit = true;
Yeah sure, put this is polling and a waste of resources. That I really don't like, especially an small battery powered systems.

No, the only possible solution I see is an additional socket connection via localhost which "signals" poll() and afterwards I can see what needs to be done (e.g. calling close()).

The reason for my question is that I need to implement an event loop based system. I need events for sockets, timers, DNS.
The idea is to use e zephyr message queue with a thread reading from the queue and calling the callback functions.


Join to automatically receive all group messages.