开发者

What is POSIX's equivalent of Win32's Mutex?

开发者 https://www.devze.com 2023-02-06 01:02 出处:网络
POSIX\'s mutex is equivalent to Win32\'s CRITICAL_SECTION -- its scope is limited to a single process. Win32\'s mute开发者_StackOverflow中文版x (Actually called a \"mutant\" in NT land) serves as a cr

POSIX's mutex is equivalent to Win32's CRITICAL_SECTION -- its scope is limited to a single process. Win32's mute开发者_StackOverflow中文版x (Actually called a "mutant" in NT land) serves as a cross process locking mechanism. What is pthreads' equivalent for cross-process locks?


It's a pthread_mutex_t with a pshared attribute set to PTHREAD_PROCESS_SHARED . However, you're responsible to place such a mutex in shared memory, that all processes can access - so it's not as simple as the win32 api.

Perhaps closer to win32 is a posix or sysv semaphore. Traditionally, synchronization across processes has also been done using file locks e.g. flock or lockf (this is in no way as slow as it might sound)


You should use IPC for cross-process operations: pipes, semaphores, message queues, or shared memory. I think in your case named semaphores would be fine. For more information:

man 7 sem_overview
0

精彩评论

暂无评论...
验证码 换一张
取 消