You mean you want to lower multiple semaphores at once? You'd have to serialize access to the semaphores by protecting them with a mutex and a wait condition. This way you can safely check values of all the semaphores and lower them sequentially without any other threads interfering.
forever {
mutex.lock();
if(s1.available() < 3 || s2.available() < 2 || ...)
waitCondition.wait(&mutex);
else {
s1.acquire(3); s2.acquire(2); ...
break;
}
mutex.unlock();
forever {
mutex.lock();
if(s1.available() < 3 || s2.available() < 2 || ...)
waitCondition.wait(&mutex);
else {
s1.acquire(3); s2.acquire(2); ...
break;
}
mutex.unlock();
To copy to clipboard, switch view to plain text mode
and then when releasing semaphores:
mutex.lock();
s1.release(2);
waitCondition.wakeAll();
mutex.unlock();
mutex.lock();
s1.release(2);
waitCondition.wakeAll();
mutex.unlock();
To copy to clipboard, switch view to plain text mode
Actually I'm not sure semaphroes are needed here at all, seems like simple ints would do.
Bookmarks