开发者

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

Can I have a sem_t (semaphore) object take a开发者_高级运维n a value of more than 1? Since sem_post only increments by 1. Is there a sem_set?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜