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. QList concurrent access and modification done right?
Forum Updated to NodeBB v4.3 + New Features

QList concurrent access and modification done right?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 2.2k Views 3 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.
  • X Offline
    X Offline
    Xumepoc
    wrote on last edited by
    #1

    I have a class with QList in it, which is integrated every 100ms and some of the elements are processed based on time parameter. After the element has been processed it is removed from the list. Now at any one moment I also need to be able to insert new elements to that list. All this is done in one thread, so we are not talking about cross thread access.

    The problem is also that I can't use QMutex and lock the list during the loop or the insertion/removal because some of the elements during their processing can also need to access the list and insert a new element.

    Is this even possible?

    1 Reply Last reply
    0
    • L Offline
      L Offline
      Leonardo
      wrote on last edited by
      #2

      I don't see a problem there. If you're using only one thread, there's no need for QMutex or worrying about concurrency.

      1 Reply Last reply
      0
      • JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3

        Hi @Xumepoc,

        A mutex is only needed when two different threads need to access the same variable. Since you only have one thread, you don't need to lock your list.

        I'm not sure what you mean by "concurrent access" if you only have 1 thread... could you explain in more detail?

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        0
        • X Offline
          X Offline
          Xumepoc
          wrote on last edited by
          #4

          The list is in a class. That classed is used in one thread. The GUI thread sends "signals" to the working thread, thus inserting new processes to the list. But at the same time, in the working thread a loop is running based on QTimer (when the timer expires it sends a signal to the slot which runs one circle at the end of which the timer is set again, so we have a loop). So in that sequence the list can be accessed randomly from the loop function and the slot for insertion, right?

          JKSHJ 1 Reply Last reply
          0
          • X Xumepoc

            The list is in a class. That classed is used in one thread. The GUI thread sends "signals" to the working thread, thus inserting new processes to the list. But at the same time, in the working thread a loop is running based on QTimer (when the timer expires it sends a signal to the slot which runs one circle at the end of which the timer is set again, so we have a loop). So in that sequence the list can be accessed randomly from the loop function and the slot for insertion, right?

            JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by
            #5

            @Xumepoc said:

            So in that sequence the list can be accessed randomly from the loop function and the slot for insertion, right?

            Right.

            Within one thread, only one slot can run at a time. Suppose your worker thread is in the middle of running the timer-expiry slot. If a signal arrives from the GUI thread, at this time, the signal's data will be put into a queue. The thread will wait for your timer-expiry slot to finish first, then it will start running your signal-from-GUI slot.

            You don't need to lock your list with a mutex.

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            0
            • X Offline
              X Offline
              Xumepoc
              wrote on last edited by
              #6

              Thanks a lot for the reply :)

              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