Question about Wait Conditions Example(Qt5.5)



  • 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;
    }
    

  • Lifetime Qt Champion

    Hi and welcome to devnet,

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



  • @SGaist Thank you!
    Now i see

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

    It will wait for mutex after waking up.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.