简体   繁体   中英

File locking a socket?

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,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM