SFML community forums

Help => Network => Topic started by: _sro5h on November 23, 2015, 05:56:41 pm

Title: Are the send and receive methods of sf::UdpSocket threadsafe?
Post by: _sro5h on November 23, 2015, 05:56:41 pm
I cant find any Information on whether I can make simultaneously calls to both sf::UdpSocket::send and sf::UdpSocket::receive on the same Socket.
In my scenario I want to have a Background thread that qeues all incoming packets and while this is happening I also would like to send out packets from this same Socket. Do I need a Mutex for that?

Best regards,
Paul
Title: Re: Are the send and receive methods of sf::UdpSocket threadsafe?
Post by: Jesper Juhl on November 23, 2015, 08:26:40 pm
As far as I know, SFML is explicitly not thread safe. So unless the documentation specifically tells you that a certain part of it is, assume it is not and do your own synchronization accordingly.
Title: Re: Are the send and receive methods of sf::UdpSocket threadsafe?
Post by: Varonth on November 25, 2015, 01:47:43 am
After some quick googling and looking at the implementation of the functions, I would say... it depends on how you call it.

To start, the actual data transfering is done by the standard function sendto() and recvfrom() which should be atomic.

The thing is the in receive remoteAddress and remotePort are called by reference so is remoteAddress in send.

Receive will also change the value of remoteAddress twice, clearing it at the beginning of the function and fill it at the end with the remoteAddress of the received datagram.

So if you use the same IpAddress instance for the remoteAddress of send and receive, you send could get the address changed when the thread is stopped and the value being changed then by receive. Besides these function arguments, everything in the two function appears to be self contained.

Atleast that is what I see at the first glance at almost 2am. Might dig into that a bit deeper tomorrow.