Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Are the send and receive methods of sf::UdpSocket threadsafe?  (Read 3092 times)

0 Members and 1 Guest are viewing this topic.

_sro5h

  • Newbie
  • *
  • Posts: 1
    • View Profile
Are the send and receive methods of sf::UdpSocket threadsafe?
« 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

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Are the send and receive methods of sf::UdpSocket threadsafe?
« Reply #1 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.

Varonth

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Are the send and receive methods of sf::UdpSocket threadsafe?
« Reply #2 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.

 

anything