nubela
nubela

Reputation: 17284

How can I Increment a POSIX sem_t semaphore by more than 1?

Can I have a sem_t (semaphore) object take an a value of more than 1? Since sem_post only increments by 1. Is there a sem_set?

Upvotes: 0

Views: 5317

Answers (1)

razlebe
razlebe

Reputation: 7144

Yes, a sem_t can take on a value of more than 1. You can use sem_init to initialise your semaphore to an abitrary value. Quoting from this link:

To initialize a semaphore, use sem_init():

  int sem_init(sem_t *sem, int pshared, unsigned int value); 
  • sem points to a semaphore object to initialize
  • pshared is a flag indicating whether or not the semaphore should be shared with fork()ed processes. LinuxThreads does not currently support shared semaphores
  • value is an initial value to set the semaphore to

Example of use:

  sem_init(&sem_name, 0, 10);

I'm not aware of any function that can increment a sem_t by an arbitrary value.

Upvotes: 1

Related Questions