As the title says, I want to use the sf::UdpSocket in blocking mode, but I need to be able to register signals for exit, and as such I also need a way to set the socket to non-blocking mode, so I can return to the function that it's using it.
if( listenerSocket.receive(packet,clientAddress,port) != sf::Socket::Done || exitSignal )
This is the line that's stopping the program. Well, obivously, it's supposed to, since the socket is set to blocking.
So, everything here is fine and dandy.
Now, I'm trying to get another thread to change the socket's blocking mode, so I can close this function.
I tried just calling listenerSocket.setBlocking(false);
from a different thread, but it doesn't seem to register it until it gets at least 1 packet while still in the blocking mode.
My question is, is there any other way to unblock this socket than just completely killing the thread it's running in?
Getting into the situation where you have to unblock a thread from another one shows flaws in the design. The only time when that should happen is when using explicit synchronization primitives (which SFML doesn't support).
You have 2 obvious solutions to this and 1 maybe not so obvious one.
The first 2 simply involve getting rid of the block in the first place, either by setting the socket non-blocking or by using an sf::SocketSelector. Either way, you will end up polling for readiness, and I don't see why you don't consider them viable options. Is there a specific reason why you want your sf::UdpSocket to be blocking? Is it a must? Or just something you prefer? The rest of your system design seems to dictate that you should use a non-blocking design, so in the end you won't have a choice anyway.
The 3rd solution, might not be the most elegant and I even consider it a bit "hacky", but in the end it "gets the job done".
Just send yourself some data on that socket and discard the packet if exitSignal is true (which is what your code seems to be doing now already).