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. Calling non-reentrant code from Multiple threads.
Qt 6.11 is out! See what's new in the release blog

Calling non-reentrant code from Multiple threads.

Scheduled Pinned Locked Moved General and Desktop
12 Posts 8 Posters 5.2k Views 1 Watching
  • 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.
  • T Offline
    T Offline
    task_struct
    wrote on last edited by
    #2

    Hello,
    take a look at "Thread-Safety":http://doc.qt.nokia.com/4.7/threads-reentrancy.html. I think it will help you :)

    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program."

    • Linu...
    1 Reply Last reply
    0
    • M Offline
      M Offline
      maxpayne
      wrote on last edited by
      #3

      The mutex should be locked inside the re-entrant function..

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SteveKing
        wrote on last edited by
        #4

        Hi,

        By "not work for me", which part doesn't work? The locking around the function looks ok, although I agree with @maxpayne the locking should be inside the function. You might have problems with the emit if you don't have an event loop running for the thread. I think you need to have an exec() in the run function somewhere.

        Steve

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #5

          emitting signals is no problem, but the connect statement must be correct (QueuedConnection!).
          Please show us some more code (creation and conbnect of the thread) and describe your problem a bit more precisely...

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dangelog
            wrote on last edited by
            #6

            Side note: if you're using the Qt naming convention, "non reentrant" means that it's not safe to call from multiple threads at all, no matter of serializing accesses (to shared data) using mutexes.

            http://developer.qt.nokia.com/wiki/Threads_Events_QObjects#ee68e5b99222bbc29a480fcb0d1d6ee2
            http://doc.qt.nokia.com/latest/threads-reentrancy.html

            Software Engineer
            KDAB (UK) Ltd., a KDAB Group company

            1 Reply Last reply
            0
            • S Offline
              S Offline
              spsingh
              wrote on last edited by
              #7

              [quote author="Gerolf" date="1305893735"]emitting signals is no problem, but the connect statement must be correct (QueuedConnection!).
              Please show us some more code (creation and conbnect of the thread) and describe your problem a bit more precisely...[/quote]

              @// GUI thread
              MyThread *thread = new MyThread();

              connect(thread, SIGNAL(done()),
              this, SLOT(Loaded()));

              thread->start();
              @
              @void MyThread::run()
              {
              sMutex.lock();
              NonReentrantFuncCall();
              sMutex.unlock();

              emit done();
              this->exec();
              }@

              Since the emit is in a different thread than the receiver and I have created an automatic connection, I need the event loop. But the above signal does not reach the slot. Is it due to the fact that the exec call is after the emit? But I can not move it above the emit as the emit will never be called. Also when do I call the exit().

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #8

                [quote author="spsingh" date="1305899696"]
                Since the emit is in a different thread than the receiver and I have created an automatic connection, I need the event loop. But the above signal does not reach the slot. Is it due to the fact that the exec call is after the emit? But I can not move it above the emit as the emit will never be called. Also when do I call the exit().
                [/quote]

                Wrong, I think. The emitting object has the same thread affinity as your receiving GUI thread, no matter if the emit was triggered from another thread. That is why the reimplement-QThread method is trickly when using signals and slots.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  situ117
                  wrote on last edited by
                  #9

                  Are you sure your code is not getting stuck while locking the mutex ?

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    dangelog
                    wrote on last edited by
                    #10

                    [quote author="spsingh" date="1305899696"]
                    Since the emit is in a different thread than the receiver and I have created an automatic connection, I need the event loop. But the above signal does not reach the slot. Is it due to the fact that the exec call is after the emit? But I can not move it above the emit as the emit will never be called. Also when do I call the exit().
                    [/quote]

                    The snippet is fine, but

                    you need an event loop in the receiver's thread, NOT in that thread. Calling exec() there seems wrong

                    what does NonReentrantFuncCall(); do exactly?

                    Software Engineer
                    KDAB (UK) Ltd., a KDAB Group company

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      dangelog
                      wrote on last edited by
                      #11

                      [quote author="Andre" date="1305925948"]
                      Wrong, I think. The emitting object has the same thread affinity as your receiving GUI thread, no matter if the emit was triggered from another thread. That is why the reimplement-QThread method is trickly when using signals and slots. [/quote]

                      Nope. It works that way. See http://developer.qt.nokia.com/wiki/Threads_Events_QObjects#913fb94dd61f1a62fc809f8d842c3afa the note about the wrong documentation.

                      Software Engineer
                      KDAB (UK) Ltd., a KDAB Group company

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        andre
                        wrote on last edited by
                        #12

                        [quote author="peppe" date="1305941696"]
                        [quote author="Andre" date="1305925948"]
                        Wrong, I think. The emitting object has the same thread affinity as your receiving GUI thread, no matter if the emit was triggered from another thread. That is why the reimplement-QThread method is trickly when using signals and slots. [/quote]

                        Nope. It works that way. See http://developer.qt.nokia.com/wiki/Threads_Events_QObjects#913fb94dd61f1a62fc809f8d842c3afa the note about the wrong documentation.

                        [/quote]
                        I stand corrected. Thanks.

                        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