In a graduate class, we've had to use semaphores to accomplish work with threads.
We were directed to use sem_init
along with a bunch of other sem_* procedure but we were not given much information about the details of each of these sem_* methods.
The prototype (and header file) of sem_init
is the following:
#include <semaphore.h>
int sem_init(sem_t *sem, int pshared, unsigned int value);
but I don't understand what the pshared value is used for. According to opengroup.org:
If the
pshared
argument has a non-zero value, then the semaphore is shared between processes; in this case, any process that can access the semaphoresem
can usesem
for performingsem_wait()
,sem_trywait()
,sem_post()
, andsem_destroy()
operations.
but I guess I don't understand the difference between say 1,2, 10, 25, 50000, etc. I think it is saying that if the value is 0 then the semaphore is not shared. (But then, what is the point?)
How do I appropriately use this pshared
parameter?