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. Are QTquick apps on android multi-threaded?
Qt 6.11 is out! See what's new in the release blog

Are QTquick apps on android multi-threaded?

Scheduled Pinned Locked Moved Solved Mobile and Embedded
14 Posts 3 Posters 3.9k Views 2 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.
  • K Offline
    K Offline
    kgregory
    wrote on last edited by
    #1

    I'm building an object method that needs to block until a signal has been received. I decided to use QMutex to accomplish this. I'm having some trouble with it though, and I'm wondering if waiting on the mutex might be causing me to miss the signal due to the app being single-threaded?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      All Qt GUI apps have at minimum 2 threads: GUI thread and main thread. You are free to create more if you need to.

      I'm building an object method that needs to block until a signal has been received.

      Signals are processed only when application returns to the event loop. If you stop execution inside a function, it will prevent event loop from operating.

      Since you are clearly in need of some asynchronous functionality, why not do this:

      • stop responding to clicks
      • return from your function immediately
      • create a slot which will respond to your signal and unblock responding to clicks

      Or something like that. I'm half-guessing how you want it to work so maybe I'm wrong.

      (Z(:^

      1 Reply Last reply
      2
      • K Offline
        K Offline
        kgregory
        wrote on last edited by kgregory
        #3

        I'm trying to integrate some C code that I inherited. The C code takes pointers to a couple of functions. One of them is a "write" function and immediately after calling "write", it calls the "read" function and expects a response (it takes some nominal amount of time for my object to receive the response, which comes from the signal).

        How do I stop responding to clicks and what will that accomplish?

        It just occurred to me that perhaps I can modify the C code so that I can return from the function immediately and the C code will retry until it gets what it wants. Although, I'm not sure that doing so will allow the application to return to the event loop... (EDIT: nevermind, I tried this and it didn't work)

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Hm, in that case either rewrite the C code so that is fits into singals-slots paradigm of Qt, or run the C code in a separate thread. Both solutions should allow you to keep the UI responsive while getting your "wait for signal" functionality.

          (Z(:^

          1 Reply Last reply
          1
          • K Offline
            K Offline
            kgregory
            wrote on last edited by kgregory
            #5

            OK I used the QThread controller/worker example (http://doc.qt.io/qt-5/qthread.html) as a model. The worker provides the interface to my C-code and the controller receives the notification signal from my BLE service.

            Since the worker can't handle incoming signals, I created a global data buffer (quint8 *) and a QMutex to pass data from controller to worker. It works like this:

            1. Worker locks the mutex
            2. Worker sends "write" signal to controller
            3. Controller receives response signal and copies response data into global data buffer
            4. Controller unlocks the mutex
            5. Worker sees unlocked mutex and reads data

            Does this sound thread-safe? I ask because it works most of the time, but my worker occasionally gets stuck waiting for the mutex. I need to verify that it's not my controller dropping a BLE response notification.

            EDIT: I just discovered that the interruption appears to be because this process suspends when my phone goes to sleep. Is there a way to allow it to continue while my phone is asleep? Alternatively, can it keep the phone from sleeping until it's done?

            sierdzioS 1 Reply Last reply
            0
            • K kgregory

              OK I used the QThread controller/worker example (http://doc.qt.io/qt-5/qthread.html) as a model. The worker provides the interface to my C-code and the controller receives the notification signal from my BLE service.

              Since the worker can't handle incoming signals, I created a global data buffer (quint8 *) and a QMutex to pass data from controller to worker. It works like this:

              1. Worker locks the mutex
              2. Worker sends "write" signal to controller
              3. Controller receives response signal and copies response data into global data buffer
              4. Controller unlocks the mutex
              5. Worker sees unlocked mutex and reads data

              Does this sound thread-safe? I ask because it works most of the time, but my worker occasionally gets stuck waiting for the mutex. I need to verify that it's not my controller dropping a BLE response notification.

              EDIT: I just discovered that the interruption appears to be because this process suspends when my phone goes to sleep. Is there a way to allow it to continue while my phone is asleep? Alternatively, can it keep the phone from sleeping until it's done?

              sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              @kgregory said in Are QTquick apps on android multi-threaded?:

              Since the worker can't handle incoming signals

              Why? It's a QObject in another thread. It can receive signals just like any other QObject (they will use the queued connection).

              Does this sound thread-safe?

              You can verify that with thread sanitizer. I don't want to claim anything here, threads are a sensitive topic, I can easily claim something that is not true.

              EDIT: I just discovered that the interruption appears to be because this process suspends when my phone goes to sleep. Is there a way to allow it to continue while my phone is asleep? Alternatively, can it keep the phone from sleeping until it's done?

              That's planform-dependent, but can be done, sure.

              • http://lists.qt-project.org/pipermail/interest/2015-August/018226.html
              • https://forum.qt.io/topic/34417/android-keep-application-running-when-device-go-to-sleep

              (Z(:^

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kgregory
                wrote on last edited by
                #7

                Why? It's a QObject in another thread. It can receive signals just like any other QObject (they will use the queued connection).

                Because it's running the C-code that is blocking, remember? This is the whole reason I had to spawn a separate thread.

                That's planform-dependent, but can be done, sure.

                awesome, thanks!

                sierdzioS 1 Reply Last reply
                0
                • K kgregory

                  Why? It's a QObject in another thread. It can receive signals just like any other QObject (they will use the queued connection).

                  Because it's running the C-code that is blocking, remember? This is the whole reason I had to spawn a separate thread.

                  That's planform-dependent, but can be done, sure.

                  awesome, thanks!

                  sierdzioS Offline
                  sierdzioS Offline
                  sierdzio
                  Moderators
                  wrote on last edited by
                  #8

                  @kgregory said in Are QTquick apps on android multi-threaded?:

                  Why? It's a QObject in another thread. It can receive signals just like any other QObject (they will use the queued connection).

                  Because it's running the C-code that is blocking, remember?

                  The signal will get into your thread's event loop queue and wait there until your C code finishes - then it will be processed. I think so, at least ;)

                  (Z(:^

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    kgregory
                    wrote on last edited by
                    #9

                    I have another question, is it possible that the QThread that I created in order to solve this problem may be recognized as a debugging feature by google play?

                    Google play isn't allowing me to upload my release build because it says it is "debuggable". See this thread for more detail: https://forum.qt.io/topic/84155/trouble-building-a-release-copy-for-android

                    1 Reply Last reply
                    0
                    • sierdzioS Offline
                      sierdzioS Offline
                      sierdzio
                      Moderators
                      wrote on last edited by
                      #10

                      I think you have not signed your .apk with the developer certificate, that might be the reason.

                      (Z(:^

                      1 Reply Last reply
                      1
                      • K Offline
                        K Offline
                        kgregory
                        wrote on last edited by
                        #11

                        How do you do that?

                        1 Reply Last reply
                        0
                        • K Offline
                          K Offline
                          kgregory
                          wrote on last edited by
                          #12

                          I opted out of google play app signing, if that's what you mean.

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

                            Hi,

                            Take a look at the Publish to GooglePlay chapter of Qt's documentation.

                            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
                            2
                            • K Offline
                              K Offline
                              kgregory
                              wrote on last edited by
                              #14

                              I should have known to look around for a document on this... 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