Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Unsolved Qthread and containers

    General and Desktop
    3
    6
    195
    Loading More Posts
    • 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
      gianc last edited by

      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 Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 Reply Quote 3
        • Chris Kawa
          Chris Kawa Moderators last edited by

          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 Reply Quote 4
          • G
            gianc last edited by

            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 Reply Quote 0
            • Chris Kawa
              Chris Kawa Moderators last edited by Chris Kawa

              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 Reply Quote 3
              • G
                gianc last edited by

                thank you, very well, very exhaustive !!!

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post