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. Thread-safe usage of QList<QString> ?
Forum Updated to NodeBB v4.3 + New Features

Thread-safe usage of QList<QString> ?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 6.9k 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.
  • C Offline
    C Offline
    c0debreak
    wrote on last edited by
    #1

    Hi,

    Is this code thread-safe? "remaining" is a QList<QString>:

    @QString target = remaining.first();

    remaining.pop_front();@

    If not, how should I work it out?


    So far I tried mutex to lock it, but after the first thread unlock the mutex, the rest of threads just wait forever: (the mutex is a private variable in the class)

    @mutex.lock(); // locked forever even unlock() processed

    QString target = remaining.first();
    remaining.pop_front();

    mutex.unlock();
    qDebug() << "Unlocked"; // I saw this message@

    1 Reply Last reply
    0
    • T Offline
      T Offline
      TioRoy
      wrote on last edited by
      #2

      Is the "mutex" object is the same for all threads? Only 1 instance?

      1 Reply Last reply
      0
      • C Offline
        C Offline
        c0debreak
        wrote on last edited by
        #3

        It's a private variable inside the class, only one instance, question updated.

        [quote author="TioRoy" date="1350530383"]Is the "mutex" object is the same for all threads? Only 1 instance?[/quote]

        1 Reply Last reply
        0
        • T Offline
          T Offline
          TioRoy
          wrote on last edited by
          #4

          One thread can call lock() multiple times?

          You can try to create the QMutex in a recursive mode ("link":http://qt-project.org/doc/qt-4.8/qmutex.html#QMutex)

          1 Reply Last reply
          0
          • K Offline
            K Offline
            KA51O
            wrote on last edited by
            #5

            You could also try to use "QReadWriteLock":http://qt-project.org/doc/qt-4.8/qreadwritelock.html or "QMutexLocker":http://qt-project.org/doc/qt-4.8/qmutexlocker.html, they are easier to work with and abolutely sufficient for your use case.

            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