Waiting for multiple semaphores without animation (C / C ++ Linux)

If I have more than one semaphore, how can I create a process block until at least one of the semaphores is free? I know I can do this with a wait loop, for example:

// blocks until one of the semaphores in sems is free, returns
// index of semaphore that was available
int multiple_sem_wait(sem_t **sems, int num_sems) {
   while (true) {
      for (int i = 0; i < num_sems; ++i) {
         if (sem_trywait(sems[i]) == 0) {
            return i;
         }
      }
   }
}

But is there a way to do this without employment? Perhaps there is some kind of IPC method other than semaphores that I should use?

thank

+4
source share
2 answers

(developers.sun.com, -) - Sun , WaitForMultipleObjects Solaris. , ( ) , . , WaitForMultipleObjects, . WaitForMultipleObjects , .

( ) , , : 1 A B, 2 A C. B 2. WaitForMultipleObjects , B C , A .

, .

+5

, :

  • .
  • ( , ) , " ".
0

All Articles