Results 1 to 4 of 4

Thread: Multiple down/up with QSemaphore

  1. #1
    Join Date
    Mar 2008
    Location
    Brazil
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question 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.
    Last edited by diogolr; 5th April 2011 at 16:45. Reason: Changing the title

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.

    Qt Code:
    1. forever {
    2. mutex.lock();
    3. if(s1.available() < 3 || s2.available() < 2 || ...)
    4. waitCondition.wait(&mutex);
    5. else {
    6. s1.acquire(3); s2.acquire(2); ...
    7. break;
    8. }
    9. mutex.unlock();
    To copy to clipboard, switch view to plain text mode 

    and then when releasing semaphores:
    Qt Code:
    1. mutex.lock();
    2. s1.release(2);
    3. waitCondition.wakeAll();
    4. 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.
    Last edited by wysota; 5th April 2011 at 21:18.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2008
    Location
    Brazil
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Multiple down/up with QSemaphore

    I understand but Qt doesn't support such a concept in a cross-platform way.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. declare an array of QSemaphore and array of slot functions
    By radeberger in forum Qt Programming
    Replies: 11
    Last Post: 2nd May 2010, 13:24
  2. Problems with Qsemaphore and QThread
    By perseo in forum Qt Programming
    Replies: 2
    Last Post: 27th August 2008, 01:21
  3. Replies: 7
    Last Post: 26th July 2008, 13:24
  4. QSemaphore: 1 Producer, 3 Consumers
    By Daedalus in forum Newbie
    Replies: 9
    Last Post: 1st April 2007, 20:59
  5. Replies: 0
    Last Post: 21st December 2006, 11:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.