If I need to synchronize two threads that both call a function with send() on a specific socket, would it be more useful to warp a critical section on the send() function or look into using a mutex? (since a socket is a kernel object)

3 Answers
3

I am assuming that this is about Windows (can't recall seeing critical section elsewhere).

It doesn't matter really which synchronization object you use if all the locking is within one process. If you want to lock across process boundary, then you should use mutex because critical section only works within single process, but named mutex can be shared between many processes.

Assuming Windows platform (that's where we have a choice between critical sections and mutexes).

Mutex (of CreateMutex) is way slower: locking and unlocking is always a system call, even if there is no contention. The cost of send, though, is likely to be enough to make this difference unnoticeable.

As pointed by another answer, mutexes can be shared between processes (if named/reopened or inherited), and critical sections are process-local.

Critical sections on windows are supposed to be faster than mutexes. Although they have more restrictions. One such restriction is critical sections can only be used inside a process.
–
drescherjmJan 16 '13 at 16:10