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. Qthread and containers

Qthread and containers

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 606 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.
  • G Offline
    G Offline
    gianc
    wrote on last edited by
    #1

    I have 2 thread and 1 qlist.
    First thread insert the objects in the qlist.
    The second thread remove the objects.
    I use QMutex and QWaitCondition for write access but I wonder: is safe ask with count(), without blocking, how many objects are in qlist?
    Thank ...

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What do you mean by safe ?
      The result might not be accurate but technically you are not modifying the container.

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

      1 Reply Last reply
      3
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        is safe ask with count(), without blocking, how many objects are in qlist?

        To add to what @SGaist said - don't do it. count() does arithmetic (basically something like end - begin) and if those are being written on another thread you will get garbage result, possibly negative numbers or something a lot larger than actual count.

        1 Reply Last reply
        4
        • G Offline
          G Offline
          gianc
          wrote on last edited by
          #4

          Thank you for yours time,
          what I would know is:
          is count() function an 'atomic operation'?
          The function count return a int value, 32 bit size (the size (o half size) of a microcontroller register ).
          if the container's elements number change, I suppose but I'm not sure, the return value can vary from +1 or -1 from previous value.
          Bye

          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by Chris Kawa
            #5

            is count() function an 'atomic operation'?

            No, it's not. The definition for it is:

            inline int count() const { return p.size(); }
            

            and p.size() is:

            inline int size() const Q_DECL_NOTHROW { return d->end - d->begin; }
            

            where begin and end are indices into an internal buffer.
            So, like I said, it does arithmetic (apart from bunch of indirections, which are definitely not atomic). It can so happen that some operation updates begin and end by shifting them. If your other thread happens to call count() before both values are written begin can happen to be larger than end and count() would result in a negative number. It's not just +/-1. It can be any number, as list elements can be added/removed in bulk.

            1 Reply Last reply
            3
            • G Offline
              G Offline
              gianc
              wrote on last edited by
              #6

              thank you, very well, very exhaustive !!!

              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