Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Thread-safe classes in Qt

Thread-safe classes in Qt

Scheduled Pinned Locked Moved Solved Mobile and Embedded
10 Posts 5 Posters 4.4k 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.
  • K Offline
    K Offline
    kumararajas
    wrote on 8 May 2017, 19:46 last edited by
    #1

    Hello all,

    This is more of generic question to understand the basics of Qt.

    What are all the classes that are thread safe and not? How do I know that? For example, is QTranslator thread safe, how do I ensure that?

    Does Qt uses any static variables? And how do I know who is using what?

    If any of the class is not thread safe, should I need to write a wrapper around that to use mutex? Is there any smart way of doing it?

    Thanks,
    Kumara

    --Kumar

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Schluchti
      wrote on 8 May 2017, 20:12 last edited by
      #2

      Have you already seen this? http://doc.qt.io/qt-4.8/threads-reentrancy.html#reentrant

      Want to read more about Qt?

      https://gympulsr.com/blog/qt/

      Latest Article: https://gympulsr.com/blog/qt/2017/06/14/ios-background-music-qt.html

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 8 May 2017, 20:15 last edited by
        #3

        Hi,

        The thread safety state (reentrant/thread safe) of each class is mentioned at the top the documentation. If nothing is written then there's no safety guaranty.

        Qt has indeed some statics, as to exactly which and for what, you'll have to take a look at the sources. do you have any specific case in mind ?

        As for mutex, semaphore and other protection, it all depends on your use 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
        1
        • K Offline
          K Offline
          kumararajas
          wrote on 8 May 2017, 20:29 last edited by kumararajas 5 Aug 2017, 20:30
          #4

          @Schluchti Yes, I did take a look at the documentation already. That wasn't so clear to me, that's why I wrote to get some more details from experts.

          @SGaist Hi Sam! I don't exactly perfect example in my mind. Here is what poked these thoughts in my mind.

          I run my Qt application in a thread and that uses QTranslator for language translation.

          In one thread I do,

          myTranslator->load("English.qm");
          qApp->installTranslator(myTranslator);
          

          In the other thread if I do

          myTranslator->removeTranslator(myTranslator);
          

          Can some other thread remove my translator? Is it practical. What if QTranslator uses some of static variables that hold some information about translator. To answer all these questions, I need to know if the class is really thread-safe.

          I know this is not a great example, but just trying to understand if one thread can update information in other thread.

          --Kumar

          J 1 Reply Last reply 9 May 2017, 04:55
          0
          • S Offline
            S Offline
            Schluchti
            wrote on 8 May 2017, 20:42 last edited by Schluchti 5 Aug 2017, 20:43
            #5

            That's probably not the answer you are looking for, but I would always add synchronization logic in case you are unsure (or the documentation doesn't provide the necessary information). Adding some synchronization logic is almost always cheap, but prevents you from running into some nasty multithreaded issues.

            I am also a big fan of grouping things logically together in one entity (thread). So for example if I need to write/read a file I create a dedicated thread that does the reading and writing. If I need to access the file from another thread, then I notify the file handling thread, which handles the action (read/write) in his own event loop (and eventually signals the result back to the thread that requested the action). In most cases you won't recognize any performance impact due to this. But keeping the logic in one thread often makes it easier to debug/extend. But that's just personal preference (and not really an answer to your question) ;)

            Want to read more about Qt?

            https://gympulsr.com/blog/qt/

            Latest Article: https://gympulsr.com/blog/qt/2017/06/14/ios-background-music-qt.html

            K 1 Reply Last reply 10 May 2017, 06:56
            0
            • K kumararajas
              8 May 2017, 20:29

              @Schluchti Yes, I did take a look at the documentation already. That wasn't so clear to me, that's why I wrote to get some more details from experts.

              @SGaist Hi Sam! I don't exactly perfect example in my mind. Here is what poked these thoughts in my mind.

              I run my Qt application in a thread and that uses QTranslator for language translation.

              In one thread I do,

              myTranslator->load("English.qm");
              qApp->installTranslator(myTranslator);
              

              In the other thread if I do

              myTranslator->removeTranslator(myTranslator);
              

              Can some other thread remove my translator? Is it practical. What if QTranslator uses some of static variables that hold some information about translator. To answer all these questions, I need to know if the class is really thread-safe.

              I know this is not a great example, but just trying to understand if one thread can update information in other thread.

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 9 May 2017, 04:55 last edited by
              #6

              @kumararajas QTranslator is not thread safe (else it would be mentioned in the documentation). Also I'm wondering why you want to use it in a thread different from the GUI thread? Keep this from documentation in mind:
              "Note that the translator must be created before the application's widgets.".

              There is one very important rule in Qt: do not use UI related classes in threads other than GUI thread!

              "Can some other thread remove my translator?" - not sure what you mean. Do you mean the myTranslator variable? If so then it depends whether it is shared across several threads.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • K Offline
                K Offline
                kumararajas
                wrote on 9 May 2017, 15:35 last edited by
                #7

                @jsulm Ya, I know, my example did not made much sense, even for me. I was just thinking about thread safe classes in Qt and lost in a ocean.

                I am bit clear now on thread safe abilities in Qt classes. Still I don't have a clear table of classes which are thread safe and which are not. But however depends on application, the class I want to use, I can go to documentation and see if that is thread safe.

                Thanks Mr. Moderator ;)

                --Kumar

                K 1 Reply Last reply 10 May 2017, 06:44
                0
                • K kumararajas
                  9 May 2017, 15:35

                  @jsulm Ya, I know, my example did not made much sense, even for me. I was just thinking about thread safe classes in Qt and lost in a ocean.

                  I am bit clear now on thread safe abilities in Qt classes. Still I don't have a clear table of classes which are thread safe and which are not. But however depends on application, the class I want to use, I can go to documentation and see if that is thread safe.

                  Thanks Mr. Moderator ;)

                  K Offline
                  K Offline
                  kshegunov
                  Moderators
                  wrote on 10 May 2017, 06:44 last edited by kshegunov 5 Oct 2017, 06:58
                  #8

                  None of the Qt classes are thread-safe, except where otherwise stated in the docs. In practice the number of fully thread-safe classes is very small - one can probably count them on the fingers of one hand and those are mostly the synchronization primitives (QMutex, QSemaphore, etc.) + some functions here and there - QObject::connect being a prime example. On the other hand, there is really very little need to have thread-safe classes in the library itself.

                  Then there's the reentrant vs non-reentrant. Most of Qt's classes (utilities, containers, QObject, etc.) are reentrant (also noted in the docs) with the major exception of the GUI stuff, which means they don't introduce and/or use statics, and it allows you to use different instances of the same class in different threads. This is usually enough. If you need you can manually sync-up reentrant classes' instances, when working across multiple threads. If the class is not reentrant, then it must be used only from a single thread (as is the case with the GUI).

                  If there's no mention if the class is reentrant and/or thread-safe then you should operate under the assumption that it is neither.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  4
                  • S Schluchti
                    8 May 2017, 20:42

                    That's probably not the answer you are looking for, but I would always add synchronization logic in case you are unsure (or the documentation doesn't provide the necessary information). Adding some synchronization logic is almost always cheap, but prevents you from running into some nasty multithreaded issues.

                    I am also a big fan of grouping things logically together in one entity (thread). So for example if I need to write/read a file I create a dedicated thread that does the reading and writing. If I need to access the file from another thread, then I notify the file handling thread, which handles the action (read/write) in his own event loop (and eventually signals the result back to the thread that requested the action). In most cases you won't recognize any performance impact due to this. But keeping the logic in one thread often makes it easier to debug/extend. But that's just personal preference (and not really an answer to your question) ;)

                    K Offline
                    K Offline
                    kshegunov
                    Moderators
                    wrote on 10 May 2017, 06:56 last edited by kshegunov 5 Oct 2017, 11:21
                    #9

                    @Schluchti said in Thread-safe classes in Qt:

                    but prevents you from running into some nasty multithreaded issues.

                    No it doesn't. Try adding synchronization primitives to the GUI classes and watch you program gloriously crash at random places. You can't make Qt's non-reentrant classes thread-safe by adding a few mutexes, as you don't have control over the execution and accesses of the internal's statics through the API.

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    3
                    • K Offline
                      K Offline
                      kumararajas
                      wrote on 10 May 2017, 21:18 last edited by
                      #10

                      Thank you very much for all the details. It helps me to understand the basics.

                      --Kumar

                      1 Reply Last reply
                      0

                      5/10

                      8 May 2017, 20:42

                      • Login

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