SDL_net 2.0 multithreading

751 views Asked by At

Is it safe to call SDL_net functions on another thread (other than the main thread)? And are there any rules about it? I could not find any information about it when I searched for it.

1

There are 1 answers

0
Kenji On BEST ANSWER

Yes, it is safe. In fact, some operations should be done in a separate thread.

I looked into the TCP part of SDL_net. In particular, any call to

  • SDLNet_ResolveHost, if it has to resolve the DNS query over a remote host
  • SDLNet_TCP_Open that connects to a remote host and doesn't just establish a listening socket
  • SDLNet_TCP_Recv if and only if there there aren't any pending bytes on the TCP stream
  • SDLNet_TCP_Send

must be done on a separate thread if you want to avoid blocking the render thread, missed timings and windows that are not responding anymore.

However, it should be avoided that two or more threads meddle with the same socket at the same time. Make sure to have the threads communicate with eachother properly to avoid bugs caused by concurrency. Use mutexes, locks, etc. to make sure of that.