Multiple down/up with QSemaphore
Some time ago, I did a software that made use of multiple (or simultaneous) UP or DOWN operations on semaphores. At that time, Qt don't had a QSemaphore class yet, so I had to do it with the OS API.
The application was dedicated to Linux, and i had no problem to do that with the semop function. I just had to pass a sembuf structure array to the function and, specify the operations (UP or DOWN) for each semaphore. The OS API assured me that the operation had to be done simultaneously, so I don't had any disturbance. If all operations couldn't be done simultaneously, the thread was blocked.
Now, facing the same problem, on a multiplataform approach (windows, linux ...), I'm thinking to use a QSemaphore class, but I don't see how can I do it, because this class have no "multiple_acquire_release" method.
P.S.: A VERY important note... I can't do the operations (down's and up's) sequentially, I have to do it simultaneously.
Can someone help me with that? :)
Regards,
Diogo.
Re: Multiple down/up with QSemaphore
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.
Code:
forever {
mutex.lock();
if(s1.available() < 3 || s2.available() < 2 || ...)
waitCondition.wait(&mutex);
else {
s1.acquire(3); s2.acquire(2); ...
break;
}
mutex.unlock();
and then when releasing semaphores:
Code:
mutex.lock();
s1.release(2);
waitCondition.wakeAll();
mutex.unlock();
Actually I'm not sure semaphroes are needed here at all, seems like simple ints would do.
Re: Multiple down/up with QSemaphore
I have already thought to "serialize" through a mutex, but what I want is really something like the semop operation on Linux API... Multiples DOWN/UP simultaneously
Clarifying my problem... What I need is a way to acquire multiple and independent resources each guarded by unique semaphores :)
Re: Multiple down/up with QSemaphore
I understand but Qt doesn't support such a concept in a cross-platform way.