Sponsored Content
Top Forums Programming pthread_mutex_init v. sem_init v. sem_get Post 302076613 by Corona688 on Wednesday 14th of June 2006 01:22:51 PM
Old 06-14-2006
A mutex is a mutal-exclusion device specifically designed to let one and only one thread have access to a certain code path at a time. If another thread tries to lock the mutex when it's already locked, that thread will be suspended until the first thread unlocks it.

Mutexes keep track of which thread "owns" them. An unlocked mutex is owned by nobody, once a thread locks it, it belongs to that thread; when they unlock it, it belongs to nobody again. Only the thread that locked the mutex is allowed to unlock it, and if it's owner tries to lock it again without unlocking it, the second lock will fail. Don't depend on this owner-checking, because some types of 'fast' mutexes dispense with it for efficiency.

here is a good example of how this kind of mutual exclusion is used -- it uses a slightly different thread API but the concept is exactly the same.

A semaphore is different from a mutex. Semaphores can store a range of values from zero to the maximum integer size, and don't track owners at all.

How a semaphore works is, when you "wait" on a sem, it attempts to vecrement the integer value. It will not let the count become negative -- if the integer value is zero, it will instead suspend the calling thread. The thread will stay suspended until something else "posts" on the semaphore, incrementing it and thus freeing the first waiting thread to2decrement it.

So if you had five threads waiting on one semaphore, you could let them go one at a time by posting to the sem 5 times.

One last difference between mutexes and semaphores is, semaphores aren't just thread-safe, they're even signal-safe. If for some reason you have to use thread synchronization inside signal handlers, they have to be semaphores. Otherwise you'll cause deadlocks.

I can't find sem_get anywhere in my system's manual pages. I'm not familiar with it.

Last edited by Corona688; 06-14-2006 at 02:28 PM..
 

We Also Found This Discussion For You

1. Programming

Undefined: sem_init, sem_post, sem_wait

Hi friends, I am using semaphores in my program, but when I compile the program, it gives the following error $ gcc sem.c -o sem -lpthread Undefined first referenced symbol in file sem_init ... (1 Reply)
Discussion started by: gabam
1 Replies
SEM_GET(3)								 1								SEM_GET(3)

sem_get - Get a semaphore id

SYNOPSIS
resource sem_get (int $key, [int $max_acquire = 1], [int $perm = 0666], [int $auto_release = 1]) DESCRIPTION
sem_get(3) returns an id that can be used to access the System V semaphore with the given $key. A second call to sem_get(3) for the same key will return a different semaphore identifier, but both identifiers access the same underlying semaphore. PARAMETERS
o $key - o $max_acquire - The number of processes that can acquire the semaphore simultaneously is set to $max_acquire. o $perm - The semaphore permissions. Actually this value is set only if the process finds it is the only process currently attached to the semaphore. o $auto_release - Specifies if the semaphore should be automatically released on request shutdown. RETURN VALUES
Returns a positive semaphore identifier on success, or FALSE on error. CHANGELOG
+--------+-----------------------------------------+ |Version | | | | | | | Description | | | | +--------+-----------------------------------------+ | 4.3.0 | | | | | | | The $auto_release parameter was added. | | | | +--------+-----------------------------------------+ NOTES
Warning When using sem_get(3) to access a semaphore created outside PHP, note that the semaphore must have been created as a set of 3 sema- phores (for example, by specifying 3 as the nsems parameter when calling the C semget() function), otherwise PHP will be unable to access the semaphore. SEE ALSO
sem_acquire(3), sem_release(3), ftok(3). PHP Documentation Group SEM_GET(3)
All times are GMT -4. The time now is 11:23 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy