I have a multi-threaded Windows C++ app written in Visual Studio 6.
Within the app 2 threads are running each trying to read UDP packets on different ports. If I protect the reading from the socket with a critical section then all the date read is fine. Without that protection data is lost from both sockets.
Is reading from a socket not thread safe? I’ve written many socket apps in the past and don’t remember having to use this sort of thread protection.
Within the app 2 threads are running each trying to read UDP packets on different ports.
How much UDP data are you sending/reading? How fast are you sending it? How much of your data is lost?
This could be a race condition… Not between the two threads, but between the thread and the socket!
I’ve seen problems in the past porting code from Linux to Windows. Windows uses (used) a default UDP buffersize of 8k. Naturally, we were sending 12k bursts, and there’s just no way to read it fast enough even with a dedicated read thread!
You can change the UDP buffersize (under Windows) with something like: