Results 1 to 8 of 8

Thread: QSempahore max reourse

  1. #1
    Join Date
    Dec 2011
    Posts
    19
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QSempahore max reourse

    Hi, is it possible to create a QSemaphore that can acquire a max number of resourse?
    For example if max is 1:
    sem.available() == 1
    sem.release()
    sem.available() == 1
    sem.acquire()
    sem.available() == 0
    sem.release()
    sem.available() == 1
    sem.release()
    sem.available() == 1
    thanks
    Teo

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

    Default Re: QSempahore max reourse

    I think you should explain better what you mean.
    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 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSempahore max reourse

    I think the OP wants a QSemaphore that will not allow QSemaphore::release() to increase the number of resources available beyond the initial allocation at construction. It is not possible to adapt QSemaphore for this (it does not remember the original allocation, just the current number) but it should be easy enough to wrap one to enforce this. I am not sure this is useful unless you are trying to cover for poor programming elsewhere. Perhaps the OP will explain further.

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

    Default Re: QSempahore max reourse

    I don't see how this would be useful. If you can discard a V operation on a semaphore then what's the point of using that semaphore... Maybe using QMutex with QWaitCondition and a regular int variable would be better...
    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.


  5. #5
    Join Date
    Dec 2011
    Posts
    19
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSempahore max reourse

    Yes, ChrisW67 the problem is correct, but I can't use QMutex or QWaitCondition, because they have not memory
    t0 -> waitCondition.wakeone


    t4 -> waitCondtion.wait -> block the thread.

    Instead I need
    t0 -> semaphore.relase


    t4 -> semaphore.acquire -> not block the thread.

    Suggestions?

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

    Default Re: QSempahore max reourse

    Qt Code:
    1. int val = 1;
    2.  
    3. m.lock();
    4. while(val == 0) c.wait(&m);
    5. val--;
    6. m.unlock();
    7.  
    8. // ...
    9.  
    10. m.lock();
    11. if(val < max)
    12. val++;
    13. c.wakeAll();
    14. m.unlock();
    To copy to clipboard, switch view to plain text mode 
    Still, I have no idea what's the point of having such a construction.
    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.


  7. #7
    Join Date
    Jun 2012
    Location
    Paris, France
    Posts
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSempahore max reourse

    If the semaphore producer already knows the consumer will have to wakeup 5 times, why not tell the consumer and let it handle the repetition while using a vanilla semaphore for synchronization?

    Creating freak synch objects is a recipe for disaster, if you want my opinion. Asynchronous programming is intrinsically full of pitfalls, no need to dig your own on top of it.

    The need for weird sync objects usually comes from weird design. In this example, I really doubt you could describe the producer-consumer semantics of your freak semaphore in plain, understandable english.

    Looks more like the "if task1 does this and it's the right moment, then I will poke task2 with a stick until it wakes up" approach. These behaviours should remain at application level, they don't belong in low-level synch objects.

  8. #8

    Default Re: QSempahore max reourse

    You might check the video to get basic understanding of the semaphore

    https://www.youtube.com/watch?v=VQFJQJGmixM

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.