Click to See Complete Forum and Search --> : pthreads & semaphore Q.
tecknophreak
03-12-2002, 10:20 AM
Is there any way to tell the value of a semaphore(syncronization type)? I checked semaphore.h and tried checking the values of __sem_value, __sem_lock.__status, and __sem_lock.__spinlock, and they were all 0 before I called sem_post. I know that my thread is waiting on this.
Danger Fan
03-12-2002, 11:35 AM
Originally posted by tecknophreak:
<STRONG>Is there any way to tell the value of a semaphore(syncronization type)? I checked semaphore.h and tried checking the values of __sem_value, __sem_lock.__status, and __sem_lock.__spinlock, and they were all 0 before I called sem_post. I know that my thread is waiting on this.</STRONG>
I think they are all zero until you sem_post them. Then they go to 1. Of course, isn't that binary semaphores?
anyway, say you have a semaphore mutex (mutual exclusion). You would want to put a wait for mutex right before you critical section of code. After the critical section, you want to post so that anyother section of code waiting for mutex can run.
sem_t mutex; //Here is your mutual exclusion sempahore
sem_init(&mutex, NULL, NULL) //initalize it
sem_wait(&mutex) //wait for mutex
//critical section of code
sem_post(&mutex) //post to free mutex for others
Of course, you mostlikely don't want to use a spin lock. They're bad, and can cause mutual exclusion and deadlock.
I don't know if I answered your question or not, but good luck.
tecknophreak
03-12-2002, 02:38 PM
crap, ok, my one thread is at the sem_wait, so i'm checking the value of it's sem and it's just incrementing.
how can this be happening? i do a sem_post about once a second, but it doesn't seem to be coming out of it.
Energon
03-12-2002, 04:02 PM
Just out of curiosity, what's the difference between a semaphore and a mutex?
bwkaz
03-12-2002, 07:23 PM
I'm pretty sure a mutex is just a semaphore whose initial value is such that one thread can call sem_wait() on it and not get blocked, but until something sem_signal()s, all other threads will be blocked.
A semaphore is a more general version of a mutex, in other words.
Energon
03-12-2002, 08:05 PM
Wow... my book that describes semaphores is really, really, really lame... everything but semaphores is great, but the impression I'm getting from it is that a mutex is a binary semaphore where locking it sets it to 0 and unlocking it sets it to 1. Either that or I'm confused on how a semaphore works (which is probably the case)...
Danger Fan
03-12-2002, 11:40 PM
my sem_init() may be wrong, maybe that's why it's incrementing. hmmm...I'm not sure.
Energon
03-13-2002, 12:59 AM
Perhaps posting some of the code would help? I was playing around with semaphores today just to see what they were about, and ran into problems of not being sure when it would block and making sure it had the right value to ensure blocking.
Though while typing this I did some research, and the good book says, "Routine sem_getvalue() returns the current value of the semaphore. Caution is advised here, since this value may change from other concurrent sem_wait() or sem_post() calls." Prototype looks like:
int sem_getvalue(sem_t* sem, int* sval);
And I think the value gets put into sval.
[ 13 March 2002: Message edited by: Energon ]
tecknophreak
03-13-2002, 11:04 AM
i'll see if i can find the relevant lines of code, it's in a program with three threads and 1000s of lines of code. :(
well, i'm looking back at the recent revisions on cvs, see if i can find something there.