Is it possible to lock a socket for a thread? For example using flock?
These locks are designed for files, but they take a generic file descriptor as a parameter.
The documentation specifically states that flock is meant for files. From the MacOS documentation page (man): (emphasis added)
Flock() applies or removes an advisory lock on the file associated with the file descriptor fd.
In particular, this is meant for inter-process synchronization on the same file. Assuming that you are trying to lock amongst threads within a process, it seems wise to instead rely on pthread_mutex_lock/unlock. (You are about to do a slow IO, so I woulnd't bother with spinlocks either).
Hope that helps,