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. How to not interrupt back ground thread and also allow foreground ui function to get executed ?
Forum Updated to NodeBB v4.3 + New Features

How to not interrupt back ground thread and also allow foreground ui function to get executed ?

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 6 Posters 1.4k Views 3 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.
  • Q Offline
    Q Offline
    Qt embedded developer
    wrote on last edited by
    #1

    i have implemented one ui . where on button click some ticket creation process happen.

    In same program i have implemented background thread using mutex which send and receive this ticket creation related details to and from server.

    So i need help regarding How to not interrupt back ground thread and also allow foreground ui function to get executed ?

    JonBJ 1 Reply Last reply
    0
    • Q Qt embedded developer

      @JonB sorry for not write clearly. i want to stop background thread run time by setting priority for foreground ui function . and how to stop and continue this thread runtime when foreground process create ticket .

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #6

      @Qt-embedded-developer said in How to not interrupt back ground thread and also allow foreground ui function to get executed ?:

      i want to stop background thread run time by setting priority for foreground ui function .

      As already said, I would not assume that even if you change a foreground thread's priority this would "stop" any background threads. Not to mention, the moment the foreground thread makes any call such as I/O it's likely to yield to the background thread running, even ignoring pre-emptive multitasking.

      and how to stop and continue this thread runtime when foreground process create ticket .

      Standard threads tend to have "pause" and "resume" functions, but I don't see those in QThread. Instead it does have QThread::requestInterruption() & QThread::isInterruptionRequested(), but these require you to write your code around them.

      1 Reply Last reply
      1
      • Q Qt embedded developer

        i have implemented one ui . where on button click some ticket creation process happen.

        In same program i have implemented background thread using mutex which send and receive this ticket creation related details to and from server.

        So i need help regarding How to not interrupt back ground thread and also allow foreground ui function to get executed ?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #2

        @Qt-embedded-developer
        Same sort of answer as in your other thread about this code you are writing.

        How to not interrupt back ground thread

        Interrupt by what? And I really don't think you're supposed to be trying to write code like this.

        and also allow foreground ui function to get executed

        That's what should be happening automatically if you do other stuff in another thread, it's the behaviour you would expect from multiple threads.

        Also, if I understand right, on the one hand you want to background thread not to get interrupted by the UI thread, and on the other hand you do want it to get interrupted so that the UI is responsive....

        Q 2 Replies Last reply
        1
        • JonBJ JonB

          @Qt-embedded-developer
          Same sort of answer as in your other thread about this code you are writing.

          How to not interrupt back ground thread

          Interrupt by what? And I really don't think you're supposed to be trying to write code like this.

          and also allow foreground ui function to get executed

          That's what should be happening automatically if you do other stuff in another thread, it's the behaviour you would expect from multiple threads.

          Also, if I understand right, on the one hand you want to background thread not to get interrupted by the UI thread, and on the other hand you do want it to get interrupted so that the UI is responsive....

          Q Offline
          Q Offline
          Qt embedded developer
          wrote on last edited by
          #3

          @JonB yes

          JonBJ 1 Reply Last reply
          0
          • Q Qt embedded developer

            @JonB yes

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #4

            @Qt-embedded-developer
            "yes" what? That you want the thread both to be interruptible and uninterruptible?

            Q 1 Reply Last reply
            0
            • JonBJ JonB

              @Qt-embedded-developer
              "yes" what? That you want the thread both to be interruptible and uninterruptible?

              Q Offline
              Q Offline
              Qt embedded developer
              wrote on last edited by
              #5

              @JonB sorry for not write clearly. i want to stop background thread run time by setting priority for foreground ui function . and how to stop and continue this thread runtime when foreground process create ticket .

              JonBJ 1 Reply Last reply
              0
              • Q Qt embedded developer

                @JonB sorry for not write clearly. i want to stop background thread run time by setting priority for foreground ui function . and how to stop and continue this thread runtime when foreground process create ticket .

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #6

                @Qt-embedded-developer said in How to not interrupt back ground thread and also allow foreground ui function to get executed ?:

                i want to stop background thread run time by setting priority for foreground ui function .

                As already said, I would not assume that even if you change a foreground thread's priority this would "stop" any background threads. Not to mention, the moment the foreground thread makes any call such as I/O it's likely to yield to the background thread running, even ignoring pre-emptive multitasking.

                and how to stop and continue this thread runtime when foreground process create ticket .

                Standard threads tend to have "pause" and "resume" functions, but I don't see those in QThread. Instead it does have QThread::requestInterruption() & QThread::isInterruptionRequested(), but these require you to write your code around them.

                1 Reply Last reply
                1
                • JonBJ JonB

                  @Qt-embedded-developer
                  Same sort of answer as in your other thread about this code you are writing.

                  How to not interrupt back ground thread

                  Interrupt by what? And I really don't think you're supposed to be trying to write code like this.

                  and also allow foreground ui function to get executed

                  That's what should be happening automatically if you do other stuff in another thread, it's the behaviour you would expect from multiple threads.

                  Also, if I understand right, on the one hand you want to background thread not to get interrupted by the UI thread, and on the other hand you do want it to get interrupted so that the UI is responsive....

                  Q Offline
                  Q Offline
                  Qt embedded developer
                  wrote on last edited by
                  #7

                  @JonB interrupt by keypad key event

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

                    Hi,

                    The question is: why do you want these interruptions in the first place ? What is the rational for that idea ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    Q 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      Hi,

                      The question is: why do you want these interruptions in the first place ? What is the rational for that idea ?

                      Q Offline
                      Q Offline
                      Qt embedded developer
                      wrote on last edited by Qt embedded developer
                      #9

                      @SGaist Because i have to allow ticket creation at highest priority while background data sending to server need to be done at lowest priority.

                      For example in bus ticket created by conductor. we can not stop conductor to create ticket. we can only interrupt the background thread to send that data to server.

                      So interruption needed at first place.

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

                        But that's the main idea of threads: there is no need to interrupt anything! Your foreground and background threads can run in parallel, both doing their job at the same time.

                        (Z(:^

                        Q 1 Reply Last reply
                        2
                        • sierdzioS sierdzio

                          But that's the main idea of threads: there is no need to interrupt anything! Your foreground and background threads can run in parallel, both doing their job at the same time.

                          Q Offline
                          Q Offline
                          Qt embedded developer
                          wrote on last edited by
                          #11

                          @sierdzio Hi !!

                          But there is one situation make my sqlite database get locked .

                          Because my back ground thread and foreground function use mutex lock that make my sqlite db to get locked . and i want to stop locking of database.

                          So How to deal with this situation ?

                          jsulmJ 1 Reply Last reply
                          0
                          • Q Qt embedded developer

                            @sierdzio Hi !!

                            But there is one situation make my sqlite database get locked .

                            Because my back ground thread and foreground function use mutex lock that make my sqlite db to get locked . and i want to stop locking of database.

                            So How to deal with this situation ?

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #12

                            @Qt-embedded-developer said in How to not interrupt back ground thread and also allow foreground ui function to get executed ?:

                            So How to deal with this situation ?

                            You expect to get clear answer without showing your code? Especially how you lock.
                            Here some hints: https://en.wikipedia.org/wiki/Deadlock#Prevention

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

                            Q 1 Reply Last reply
                            1
                            • jsulmJ jsulm

                              @Qt-embedded-developer said in How to not interrupt back ground thread and also allow foreground ui function to get executed ?:

                              So How to deal with this situation ?

                              You expect to get clear answer without showing your code? Especially how you lock.
                              Here some hints: https://en.wikipedia.org/wiki/Deadlock#Prevention

                              Q Offline
                              Q Offline
                              Qt embedded developer
                              wrote on last edited by
                              #13

                              @jsulm In my case deadlock situation happen due to mutual exclusion because the foreground and background hold one resource sqlite database.

                              And so database get locked .

                              I want to avoid this situation.

                              JonBJ 1 Reply Last reply
                              0
                              • Q Qt embedded developer

                                @jsulm In my case deadlock situation happen due to mutual exclusion because the foreground and background hold one resource sqlite database.

                                And so database get locked .

                                I want to avoid this situation.

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by
                                #14

                                @Qt-embedded-developer said in How to not interrupt back ground thread and also allow foreground ui function to get executed ?:

                                one resource sqlite database

                                Before commenting on what you have said, could you clarify: do you mean a SQLite database file which is supplied and opened as a Qt resource file, or do you mean "resource" in a broad sense and you are just talking about an external SQLite file, not a Qt resource?

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

                                  Sounds like the solution would be to keep the SQLite DB opened by only one thread, which should in turn distribute the data as necessary to other parts of the application, for example using signals and slots. No locking and no mutexes :-)

                                  BTW. be very careful with SQLite and threads, by default it does not support them. You need to compile SQLite yourself. See https://sqlite.org/threadsafe.html

                                  (Z(:^

                                  Q S 2 Replies Last reply
                                  3
                                  • JonBJ JonB

                                    @Qt-embedded-developer said in How to not interrupt back ground thread and also allow foreground ui function to get executed ?:

                                    one resource sqlite database

                                    Before commenting on what you have said, could you clarify: do you mean a SQLite database file which is supplied and opened as a Qt resource file, or do you mean "resource" in a broad sense and you are just talking about an external SQLite file, not a Qt resource?

                                    Q Offline
                                    Q Offline
                                    Qt embedded developer
                                    wrote on last edited by
                                    #16

                                    @JonB
                                    I mean "resource" in a broad sense and i am talking about an external SQLite file, not a qt resource

                                    1 Reply Last reply
                                    0
                                    • sierdzioS sierdzio

                                      Sounds like the solution would be to keep the SQLite DB opened by only one thread, which should in turn distribute the data as necessary to other parts of the application, for example using signals and slots. No locking and no mutexes :-)

                                      BTW. be very careful with SQLite and threads, by default it does not support them. You need to compile SQLite yourself. See https://sqlite.org/threadsafe.html

                                      Q Offline
                                      Q Offline
                                      Qt embedded developer
                                      wrote on last edited by
                                      #17

                                      @sierdzio Thanks for Help ..

                                      1 Reply Last reply
                                      0
                                      • sierdzioS sierdzio

                                        Sounds like the solution would be to keep the SQLite DB opened by only one thread, which should in turn distribute the data as necessary to other parts of the application, for example using signals and slots. No locking and no mutexes :-)

                                        BTW. be very careful with SQLite and threads, by default it does not support them. You need to compile SQLite yourself. See https://sqlite.org/threadsafe.html

                                        S Offline
                                        S Offline
                                        SimonSchroeder
                                        wrote on last edited by
                                        #18

                                        @sierdzio said in How to not interrupt back ground thread and also allow foreground ui function to get executed ?:

                                        BTW. be very careful with SQLite and threads, by default it does not support them. You need to compile SQLite yourself. See https://sqlite.org/threadsafe.html

                                        The link you provided states that by default SQLite uses serialized access, i.e. it is thread safe by default.

                                        @Qt-embedded-developer You should not use mutexes to access SQLite. With the proper settings multiple connections to the same SQLite file can be open. SQLite will do all the synchronization. You might get into trouble if one of the threads reads quite often and the other thus cannot lock it for writing.

                                        Maybe you can clarify what you are actually trying to do. You should not use a database to communicate between two threads (one polling if there is something new to be sent over the network). My guess is that you don't need mutexes at all. In order for the GUI thread to communicate that there is something new to be sent, just use signals and slots between the two threads to communicate. All requests from the GUI will be queued inside the event loop of the network thread and handled one after the other. This is how Qt should be used.

                                        1 Reply Last reply
                                        3

                                        • Login

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