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. QWaitCondition issue

QWaitCondition issue

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 660 Views 1 Watching
  • 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.
  • M Offline
    M Offline
    moravas
    wrote on last edited by
    #1

    Hi Folks,

    I have an unit test code, which should test the thread safety behavioral of a test class. The testcode is the following:
    @
    public: threadWorker() {
    }

    public: virtual void run();

    public: static const int logCountPerThread;
    public: static const int threadCount;
    public: static QWaitCondition waitCondition;
    public: static cacheType logs;

    private: QMutex _sync;
    private: QMutex _end;

    private: cacheType _cache;
    };
    @

    The definition of the static variables:
    @
    cacheType threadWorker::logs;
    const int threadWorker::threadCount = 100;
    QWaitCondition threadWorker::waitCondition;
    const int threadWorker::logCountPerThread = 10000;
    @

    and the worker method itself:
    @
    void threadWorker::run() {
    _sync.lock();
    qDebug() << "thread started, and _sync locked";
    waitCondition.wait(&_sync);

    qDebug() << "woke up";
    for(int count = 0; count < logCountPerThread; count++) {
        _cache.insert(core::logging::log(core::logging::logSeverity::info, QString()).id, 0);
    }
    
    QMutexLocker lock(&_end);
    qDebug() << "_end locked";
    for(cacheType::iterator it = _cache.begin(); it != _cache.end(); ++it) {
        logs.insert(it.key(), 0);
    }
    
    _cache.clear();
    _sync.unlock();
    

    }
    @
    I deploy the threads from the following method:
    @
    void logTest::concurentInsatnce() {
    QVector<threadWorker*> pool;
    for(int count = 0; count < threadWorker::threadCount; count++) {
    pool.push_back(new threadWorker());
    pool.last()->start();
    }

    threadWorker::waitCondition.wakeAll();
    qDebug() << "wake all sent";
    
    for(threadWorker* thread: pool) {
        qDebug() << "start for waiting thread";
        thread->wait();
        qDebug() << "thread exited";
    }
    
    QVERIFY2(threadWorker::logs.size() == threadWorker::logCountPerThread * threadWorker::threadCount,   "unequal object count");
    

    }
    @
    (OFF: I'm sorry for the lot's of debug outputs)

    I observed two main error cases:

    • I start only one thread by adjusting the value of threadWorker::threadCount. In this case, it seems, that the execution is terminated at the "waitCondition.wait(&_sync);", however the debugger output tell, that the wakeAll() signal sent.
    • another error case is the current state, what you can see: I start 100 threads. In this case, I see, that few threads are finising its work, and exit normaly, but another threads "don't see" the wakeAll.

    Can anybody help me?

    Regards,
    Norbert

    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