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. Is insert function of QSet thread-safe?
QtWS25 Last Chance

Is insert function of QSet thread-safe?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 2.0k 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.
  • A Offline
    A Offline
    Aaron Kim
    wrote on 27 Aug 2018, 05:47 last edited by
    #1

    0_1535348271161_capture.PNG

    Segmentation fault occured inner QHash function.
    Because CountDataPixel class inherits QThread, I conjectured that something bad happens If I use QSet insert with multiple threads.
    This is what I think : Even though QPair(x1, y1), QPair(x2, y2) are different, they are calculated in the same hash value. If two threads insert each QPair simultaneously, confliction occurs.

    Am I correct? If not, why does segmentation fault occur in the inner hash function?

    J 1 Reply Last reply 27 Aug 2018, 08:09
    0
    • A Aaron Kim
      27 Aug 2018, 05:47

      0_1535348271161_capture.PNG

      Segmentation fault occured inner QHash function.
      Because CountDataPixel class inherits QThread, I conjectured that something bad happens If I use QSet insert with multiple threads.
      This is what I think : Even though QPair(x1, y1), QPair(x2, y2) are different, they are calculated in the same hash value. If two threads insert each QPair simultaneously, confliction occurs.

      Am I correct? If not, why does segmentation fault occur in the inner hash function?

      J Offline
      J Offline
      JKSH
      Moderators
      wrote on 27 Aug 2018, 08:09 last edited by JKSH
      #2

      @Aaron-Kim said in Is insert function of QSet thread-safe?:

      Is insert function of QSet thread-safe?

      No, it is not thread-safe.

      I conjectured that something bad happens If I use QSet insert with multiple threads.

      That is correct.

      This is what I think : Even though QPair(x1, y1), QPair(x2, y2) are different, they are calculated in the same hash value. If two threads insert each QPair simultaneously, confliction occurs.

      Am I correct?

      No. It doesn't matter if the 2 pairs have the same hash value or different hash values.

      If you try to modify the same object from 2 different threads at the same time, they can overwrite parts of each other and corrupt your data. Because QSet is not thread-safe, you must lock it and make sure that only 1 thread can read/write it at any given time.

      See http://doc.qt.io/qt-5/threads-synchronizing.html

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

      A 1 Reply Last reply 27 Aug 2018, 09:09
      4
      • J JKSH
        27 Aug 2018, 08:09

        @Aaron-Kim said in Is insert function of QSet thread-safe?:

        Is insert function of QSet thread-safe?

        No, it is not thread-safe.

        I conjectured that something bad happens If I use QSet insert with multiple threads.

        That is correct.

        This is what I think : Even though QPair(x1, y1), QPair(x2, y2) are different, they are calculated in the same hash value. If two threads insert each QPair simultaneously, confliction occurs.

        Am I correct?

        No. It doesn't matter if the 2 pairs have the same hash value or different hash values.

        If you try to modify the same object from 2 different threads at the same time, they can overwrite parts of each other and corrupt your data. Because QSet is not thread-safe, you must lock it and make sure that only 1 thread can read/write it at any given time.

        See http://doc.qt.io/qt-5/threads-synchronizing.html

        A Offline
        A Offline
        Aaron Kim
        wrote on 27 Aug 2018, 09:09 last edited by Aaron Kim
        #3

        @JKSH Thanks for reply ! I just change it QSet to QQueue and move all the elements from QSet to QQueue after multithreaded task. (Even though it is a little bit slower)

        A 1 Reply Last reply 27 Aug 2018, 09:59
        0
        • A Aaron Kim
          27 Aug 2018, 09:09

          @JKSH Thanks for reply ! I just change it QSet to QQueue and move all the elements from QSet to QQueue after multithreaded task. (Even though it is a little bit slower)

          A Offline
          A Offline
          Asperamanca
          wrote on 27 Aug 2018, 09:59 last edited by
          #4

          @Aaron-Kim said in Is insert function of QSet thread-safe?:

          @JKSH Thanks for reply ! I just change it QSet to QQueue and move all the elements from QSet to QQueue after multithreaded task. (Even though it is a little bit slower)

          Interesting. Because QQueue isn't thread-safe, either. Unless you added a lock, or use separate queues for each thread, you have just hidden the problem, and it may pop up again at any time.

          A 1 Reply Last reply 27 Aug 2018, 12:11
          3
          • A Asperamanca
            27 Aug 2018, 09:59

            @Aaron-Kim said in Is insert function of QSet thread-safe?:

            @JKSH Thanks for reply ! I just change it QSet to QQueue and move all the elements from QSet to QQueue after multithreaded task. (Even though it is a little bit slower)

            Interesting. Because QQueue isn't thread-safe, either. Unless you added a lock, or use separate queues for each thread, you have just hidden the problem, and it may pop up again at any time.

            A Offline
            A Offline
            Aaron Kim
            wrote on 27 Aug 2018, 12:11 last edited by
            #5

            @Asperamanca Isn't it thread safe if I insert only different elements, without any other operations? (Order of elements is not a problem)

            A J 2 Replies Last reply 27 Aug 2018, 12:15
            0
            • A Aaron Kim
              27 Aug 2018, 12:11

              @Asperamanca Isn't it thread safe if I insert only different elements, without any other operations? (Order of elements is not a problem)

              A Offline
              A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on 27 Aug 2018, 12:15 last edited by
              #6

              @Aaron-Kim

              Isn't it thread safe if I insert only different elements, without any other operations?

              You are assuming internal details about "black box" containers. Don't do that.

              The documentation does not write that QSet or QQueue are thread-safe, so they aren't.

              Even if it works for you, it may not work on another platform or another Qt version.

              Regards

              Qt has to stay free or it will die.

              A 1 Reply Last reply 27 Aug 2018, 16:13
              2
              • A aha_1980
                27 Aug 2018, 12:15

                @Aaron-Kim

                Isn't it thread safe if I insert only different elements, without any other operations?

                You are assuming internal details about "black box" containers. Don't do that.

                The documentation does not write that QSet or QQueue are thread-safe, so they aren't.

                Even if it works for you, it may not work on another platform or another Qt version.

                Regards

                A Offline
                A Offline
                Aaron Kim
                wrote on 27 Aug 2018, 16:13 last edited by
                #7

                @aha_1980 I saw that someone said that STL is thread-safe for writing different objects in this post.
                I thought that this applies to QTL either.

                A 1 Reply Last reply 27 Aug 2018, 16:54
                0
                • A Aaron Kim
                  27 Aug 2018, 16:13

                  @aha_1980 I saw that someone said that STL is thread-safe for writing different objects in this post.
                  I thought that this applies to QTL either.

                  A Offline
                  A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on 27 Aug 2018, 16:54 last edited by
                  #8

                  @Aaron-Kim they said writing on different objects is safe, they did not talk about elements of the same object.

                  of course that should apply for almost every container, includin Qt's.

                  Regards

                  Qt has to stay free or it will die.

                  1 Reply Last reply
                  1
                  • A Aaron Kim
                    27 Aug 2018, 12:11

                    @Asperamanca Isn't it thread safe if I insert only different elements, without any other operations? (Order of elements is not a problem)

                    J Offline
                    J Offline
                    JKSH
                    Moderators
                    wrote on 27 Aug 2018, 22:54 last edited by
                    #9

                    First, let's be clear about terminology. The important names are:

                    • Container: An object that can hold multiple other objects (or primitives)
                    • Element: An object (or primitive) that is stored inside a container

                    @Aaron-Kim said in Is insert function of QSet thread-safe?:

                    someone said that STL is thread-safe for writing different objects in this post.

                    The accepted answer in that post talks about modifying existing elements inside a container.

                    Inserting does not fit this criteria because insert() does not modify an existing element. Instead, it modifies the container itself! Therefore, inserting into an STL container is definitely not thread-safe.

                    @aha_1980 said in Is insert function of QSet thread-safe?:

                    @Aaron-Kim they said writing on different objects is safe, they did not talk about elements of the same object.

                    The C++ standard does say "implementations are required to avoid data races when the contents of the contained object in different elements in the same sequence, excepting vector<bool>, are modified concurrently."

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

                    1 Reply Last reply
                    1
                    • A Offline
                      A Offline
                      Aaron Kim
                      wrote on 28 Aug 2018, 04:54 last edited by
                      #10

                      Thanks for reply! I got it.

                      1 Reply Last reply
                      0

                      8/10

                      27 Aug 2018, 16:54

                      • Login

                      • Login or register to search.
                      8 out of 10
                      • First post
                        8/10
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved