Results 1 to 2 of 2

Thread: Understanding Semaphore Example

  1. #1
    Join Date
    May 2016
    Posts
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Question Understanding Semaphore Example

    Hi,
    I don't understand some details about the Semaphores Example QSemaphore.

    For simplicity I'll paste the example code:

    Global Variables
    Qt Code:
    1. const int DataSize = 100000;
    2.  
    3. const int BufferSize = 8192;
    4. char buffer[BufferSize];
    5.  
    6. QSemaphore freeBytes(BufferSize);
    7. QSemaphore usedBytes;
    To copy to clipboard, switch view to plain text mode 

    Producer Class
    Qt Code:
    1. class Producer : public QThread
    2. {
    3. public:
    4. void run() Q_DECL_OVERRIDE
    5. {
    6. qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
    7. for (int i = 0; i < DataSize; ++i) {
    8. freeBytes.acquire();
    9. buffer[i % BufferSize] = "ACGT"[(int)qrand() % 4];
    10. usedBytes.release();
    11. }
    12. }
    13. };
    To copy to clipboard, switch view to plain text mode 

    Consumer Class

    Qt Code:
    1. class Consumer : public QThread
    2. {
    3. Q_OBJECT
    4. public:
    5. void run() Q_DECL_OVERRIDE
    6. {
    7. for (int i = 0; i < DataSize; ++i) {
    8. usedBytes.acquire();
    9. fprintf(stderr, "%c", buffer[i % BufferSize]);
    10. freeBytes.release();
    11. }
    12. fprintf(stderr, "\n");
    13. }
    14.  
    15. signals:
    16. void stringConsumed(const QString &text);
    17.  
    18. protected:
    19. bool finish;
    20. };
    To copy to clipboard, switch view to plain text mode 

    The questions are:
    • does freeBytes(BufferSize) reserves BufferSize resources or not?
    • how the consumer is blocked waiting for the usedBytes semaphore to be released?
    • The example say "Once the producer has put one byte in the buffer, freeBytes.available() is BufferSize - 1 and usedBytes.available() is 1" why the freeBytes is decremented and the usedBytes semaphore is incremented?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Understanding Semaphore Example

    Quote Originally Posted by KingDavid View Post
    does freeBytes(BufferSize) reserves BufferSize resources or not?
    Yes, it does.

    Quote Originally Posted by KingDavid View Post
    how the consumer is blocked waiting for the usedBytes semaphore to be released?
    usedBytes is initialized with 0 resources (constructor argument default value), any require() on it will block until enough resources have been made available with respective release() calls.

    Quote Originally Posted by KingDavid View Post
    The example say "Once the producer has put one byte in the buffer, freeBytes.available() is BufferSize - 1 and usedBytes.available() is 1" why the freeBytes is decremented and the usedBytes semaphore is incremented?
    freeBytes is decremented because acquire() is being called. Its argument has a default value of 1, so it is decremented by 1.
    Similar for usedBytes, its release() is called, again default value being 1, so it increments by 1.

    Cheers,
    _

Similar Threads

  1. cannot create semaphore
    By Svaths in forum Qt for Embedded and Mobile
    Replies: 4
    Last Post: 22nd May 2014, 08:30
  2. Not understanding QtScript
    By zephod in forum Newbie
    Replies: 5
    Last Post: 19th April 2012, 15:43
  3. Understanding QNetworkConfigurationManager
    By TheIndependentAquarius in forum Qt Programming
    Replies: 5
    Last Post: 21st September 2011, 12:47
  4. Replies: 16
    Last Post: 17th May 2011, 16:02
  5. Error: "Cannot create semaphore ..."?
    By vkincaid in forum Qt Programming
    Replies: 3
    Last Post: 21st October 2009, 15:32

Tags for this Thread

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.