In my Linux app, I have two threads that both try to send a UDP broadcast packet (around 50-500 bytes) using the same UDP client socket. They do this about once every 2-3 seconds. In this case, around the “send(…)” clause, I could put pthread_mutex_lock or pthread_spin_lock. Theory says that if it’s a very small operation, a pthread_spin_lock is more efficient (despite high CPU consumption for that small amount of time). But if its a larger operation, then pthread_mutex_lock is better.
Is sending a UDP packet considered “small enough” to warrant using a pthread_spin_lock, or should I still stick with pthread_mutex_lock?
Thanks
If the only need for locking is because they’re both sending on the same socket, then there’s no need for locking at all – it’s acceptable for two threads to call
send()on the same UDP socket at the same time. The data sent won’t be interleaved.