Billy ONeal
Billy ONeal

Reputation: 106609

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

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

Upvotes: 5

Views: 1379

Answers (2)

nos
nos

Reputation: 229264

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)

Upvotes: 6

Shlomi Loubaton
Shlomi Loubaton

Reputation: 732

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

Upvotes: 0

Related Questions