user140053
user140053

Reputation:

OpenMutex and pthread

I noticed that, under pthread library, there is no equivalent to function like OpenMutex() under Win32, or semget() with semaphore.

Why ?

Does it mean I have to use pthread_create() in any case ?

Upvotes: 0

Views: 302

Answers (1)

selbie
selbie

Reputation: 104569

Creating or accessing a kernel object (mutex, event, semaphore) with a string name in Win32 is usually only intended for cross-process synchronization.

In Win32, For synchronizing within the same process, the appropriate pattern is to the create the Win32 object once, and just share its HANDLE with the different threads that need to access it. But if your lock doesn't need to beyond threads of the same process, it's usually more efficient to use a CRITICAL_SECTION object.

Synchronization objects created by pthreads are can only be shared within the same process. So if you create a mutex in pthreads - it's only meant to be shared with other threads in the same process.

If you need to create a cross-process lock in Unix, you'll have to use another appropriate mechanism (i.e. semget and friends).

Upvotes: 1

Related Questions