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

Author Topic: Using sf::Lock on UDP sockets  (Read 2911 times)

0 Members and 1 Guest are viewing this topic.

FleshyOverlord

  • Newbie
  • *
  • Posts: 22
    • View Profile
Using sf::Lock on UDP sockets
« on: January 16, 2020, 04:50:01 am »
Hello, I am working on a tank game and am trying to use both TCP and UDP. So far the TCP is working however I am having difficulties getting UDP to work do to some confusion about mutex. Is it safe to use sf::Lock before calling receive on a blocking UDP socket, or will this stall the program?

                sf::Packet* uPack = new sf::Packet();
                sf::IpAddress rcvAddr;
                short unsigned int rcvPort;
                PacketCont* packCont = new PacketCont();
                sf::Lock lock(sManager->mutex);//this is the line I am worried about
                sf::Socket::Status status = m_UDPSocket.receive(*uPack, rcvAddr, rcvPort);
 
Note: I need the mutex since I am using multiple threads which access the UDP socket class.
« Last Edit: January 16, 2020, 04:53:26 am by FleshyOverlord »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Using sf::Lock on UDP sockets
« Reply #1 on: January 16, 2020, 08:37:36 am »
The mutex will be locked until the UDP socket receives something. Whether or not this may be long depends on your program. Whether or not this is good or bad depends on what your other threads do.

You know that any attempt to access the socket while it is stuck in the `receive`call will block the calling thread, now it's up to you to adjust your code accordingly if needed ;)
Laurent Gomila - SFML developer

FleshyOverlord

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Using sf::Lock on UDP sockets
« Reply #2 on: January 16, 2020, 02:29:01 pm »
Thank you Laurent!

 

anything