Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Question about Wait Conditions Example(Qt5.5)

Question about Wait Conditions Example(Qt5.5)

Scheduled Pinned Locked Moved General and Desktop
qwaitconditionqmutexqt5.5
3 Posts 2 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sigmaN
    wrote on last edited by
    #1

    Hi everyone!
    I am in doubt about one important thing in using QMutex and QWaitCondition.

    1.Consumer thread readed out all bytes and goes to wait state(blocked). Mutex is unlocked now, because calling bufferNotEmpty.wait() blocks the thread and unlocks mutex.

    mutex.lock();
    if (numUsedBytes == 0)
        bufferNotEmpty.wait(&mutex);
    mutex.unlock();
    
    1. Consumer waits, then Producer thread locks the mutex(it's ok because mutex was unlocked), increments ++numUsedBytes, and calls bufferNotEmpty.wakeAll()!
                mutex.lock();
                ++numUsedBytes;
                bufferNotEmpty.wakeAll();
                mutex.unlock();
    
    1. From this point we have mutex already locked by Producer and Consumer thread is waking up.
      QWaitCondition::wait() doc says The lockedMutex will be returned to the same locked state. This function is provided to allow the atomic transition from the locked state to the wait state.

    My question: As we know from docs, Consumer thread will try to lock the mutex before returning from QWaitCondition::wait() . Does this mean it will wait for mutex to be unlocked by producer first?
    So wait actually doing two waits: wait for condition and then wait for mutex. Am i right?

    This is Wait Conditions Example from Qt Creator help. Here is full code:

    const int DataSize = 100000;
    const int BufferSize = 8192;
    char buffer[BufferSize];
    
    QWaitCondition bufferNotEmpty;
    QWaitCondition bufferNotFull;
    QMutex mutex;
    int numUsedBytes = 0;
    
    class Producer : public QThread
    {
    public:
        Producer(QObject *parent = NULL) : QThread(parent)
        {
        }
    
        void run() Q_DECL_OVERRIDE
        {
            qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
    
            for (int i = 0; i < DataSize; ++i) {
                mutex.lock();
                if (numUsedBytes == BufferSize)
                    bufferNotFull.wait(&mutex);
                mutex.unlock();
    
                buffer[i % BufferSize] = "ACGT"[(int)qrand() % 4];
    
                mutex.lock();
                ++numUsedBytes;
                bufferNotEmpty.wakeAll();
                mutex.unlock();
            }
        }
    };
    
    class Consumer : public QThread
    {
        Q_OBJECT
    public:
        Consumer(QObject *parent = NULL) : QThread(parent)
        {
        }
    
        void run() Q_DECL_OVERRIDE
        {
            for (int i = 0; i < DataSize; ++i) {
                mutex.lock();
                if (numUsedBytes == 0)
                    bufferNotEmpty.wait(&mutex);
                mutex.unlock();
    
                fprintf(stderr, "%c", buffer[i % BufferSize]);
    
                mutex.lock();
                --numUsedBytes;
                bufferNotFull.wakeAll();
                mutex.unlock();
            }
            fprintf(stderr, "\n");
        }
    
    signals:
        void stringConsumed(const QString &text);
    };
    
    int main(int argc, char *argv[])
    {
        QCoreApplication app(argc, argv);
        Producer producer;
        Consumer consumer;
        producer.start();
        consumer.start();
        producer.wait();
        consumer.wait();
        return 0;
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      To answer your questions, the best is to look at the source of QWaitCondition (unix)

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        To answer your questions, the best is to look at the source of QWaitCondition (unix)

        S Offline
        S Offline
        sigmaN
        wrote on last edited by
        #3

        @SGaist Thank you!
        Now i see

        bool returnValue = d->wait(time);
        mutex->lock();
        

        It will wait for mutex after waking up.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved