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 it necessary to synchronise results passed across threads with signals?
QtWS25 Last Chance

Is it necessary to synchronise results passed across threads with signals?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 1.2k 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.
  • L Offline
    L Offline
    lisabeeren
    wrote on last edited by
    #1

    Hi,

    My program loads a file in my file loader object. I move the file loader object to a different thread, so it does not block the UI thread. the file loader emits signals about the progress of the loading, and eventually emits a 'load complete' signal which passes a pointer reference to an object representing the contents of that file. This signal is of the form:

    @signals:
    DataSetLoaded(DataSet *dataSet)@

    I'm wondering if I need to do any synchronization before I read the dataSet object from the main thread, or does the cross thread Qt signal/slot thing do that for me automagically? or do I need to do it myself in the case of pointers?

    I'm not doing it now, and it works for me, but I have had some reports of crashes which would be consistent with a race condition from failed synchronization.

    with thanks

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

      Hi,

      You have to do synchronization if your threads are both accessing a same resource at the same time. I guess once your loading thread emit the signal with the dataset as argument, the loading process is finished, and the thread will not access the data anymore? In such a case, you don't need to do synchronization because there is no concurrent access on a same resource. If your loading thread can still access the dataset, you should consider using QMutex.

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

        Hi,

        You are passing a pointer to a "remote" object and you access it from several thread. Then yes, you have to protect the access to that object. Qt's signals & slots won't help in this case.

        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
        0
        • L Offline
          L Offline
          lisabeeren
          wrote on last edited by
          #4

          bq. I guess once your loading thread emit the signal with the dataset as argument, the loading process is finished, and the thread will not access the data anymore? In such a case, you don’t need to do synchronization because there is no concurrent access on a same resource.

          I think that there still needs to be a single synchronization step between the two threads when the signal is emited to ensure they see the same thing in memory (even in the absence of concurrent access). I'm wondering if this is provided automatically by Qt.

          Once the DataSetLoaded(DataSet *dataSet) signal is emitted, the loader thread no longer touches the dataSet object, so there is no need for ongoing synchronization.

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

            If you can guarantee that your code has no concurrent access, then you don't have to worry about synchronizing anything.

            Anyway, Qt copies the signal parameters from the signalling thread into the slot thread safely. It does this by using queued connections. See the "ConnectionType":http://qt-project.org/doc/qt-5/qt.html#ConnectionType-enum documentation.

            You might also be interested in reading the "Synchronizing Threads":http://qt-project.org/doc/qt-5/threads-synchronizing.html article.

            [quote author="lisabeeren" date="1410876476"]I think that there still needs to be a single synchronization step between the two threads when the signal is emited to ensure they see the same thing in memory (even in the absence of concurrent access).[/quote]No need. The slot will only see your DataSet pointer after you emit the signal. Just make sure you don't change the DataSet object after you emit the signal -- then your slot will always see the final values in the DataSet.

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

            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