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. Handling Database on Multiple Thread

Handling Database on Multiple Thread

Scheduled Pinned Locked Moved Solved General and Desktop
qthreadsql databasec++multithreaddebugging
13 Posts 3 Posters 2.7k 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.
  • Christian EhrlicherC Christian Ehrlicher

    Using a QThread for a single insert statement doesn't look very effective. The same goes for the serial communication - QSerialPort is async - no thread needed.

    M Offline
    M Offline
    mvsri
    wrote on last edited by mvsri
    #3

    @Christian-Ehrlicher There are multiple insert statements and I am using open close principal for database connection.
    Regarding Serialport Qt serial blocking example
    I referred this blocking example and I read and write data from serial thread and also planning to use wait conditions.

    1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #4

      delay logic is troublesome because it makes assumptions about response times, sometimes needlessly, and other times not long enough. Avoid that kind of programming when possible, and favor event sycronization instead.

      M 1 Reply Last reply
      2
      • Kent-DorfmanK Kent-Dorfman

        delay logic is troublesome because it makes assumptions about response times, sometimes needlessly, and other times not long enough. Avoid that kind of programming when possible, and favor event sycronization instead.

        M Offline
        M Offline
        mvsri
        wrote on last edited by
        #5

        @Kent-Dorfman For serial communication most preferable way is to use async code with signals and slots. Understood the point here.
        But can you tell me when is preferred way to use above blocking example.

        Regarding Database connection can I use the thread where there are multiple insert statements and I’m using open close method here.

        Thank you!

        1 Reply Last reply
        0
        • Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote on last edited by
          #6

          open/close is expensive. insert/select based on prepared statements is cheap. move the open/close logic out of the thread and do mutext blocked sql functions if you chose a single connect. otherwise create a connection pool and have your thread use a free connection from the pool for every concurrent sql transaction.

          implementation is left as an exercise for the OP.

          M 1 Reply Last reply
          3
          • Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #7

            Even two or thread statements don't need a separate thread. Especially when the open/close is done in the run function as @Kent-Dorfman pointed out.
            Write a proper class which opens the db in the ctor and closes it in the dtor, implement the insert statements in a slot() and after you found out that this causes performance problems (which I doubt) then move this object to another thread.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            M 1 Reply Last reply
            1
            • Christian EhrlicherC Christian Ehrlicher

              Even two or thread statements don't need a separate thread. Especially when the open/close is done in the run function as @Kent-Dorfman pointed out.
              Write a proper class which opens the db in the ctor and closes it in the dtor, implement the insert statements in a slot() and after you found out that this causes performance problems (which I doubt) then move this object to another thread.

              M Offline
              M Offline
              mvsri
              wrote on last edited by
              #8

              @Christian-Ehrlicher Thank you for the suggestion I will implement it and check the result.
              Although before proceeding my query is since I'm reading data from serialthread and emitting it to mainscreen and in mainscreen I'm displaying the values as well as storing the values in the database using databasethread. My point here is should I use a locking mechanism here when handling the data between threads?

              Christian EhrlicherC 1 Reply Last reply
              0
              • Kent-DorfmanK Kent-Dorfman

                open/close is expensive. insert/select based on prepared statements is cheap. move the open/close logic out of the thread and do mutext blocked sql functions if you chose a single connect. otherwise create a connection pool and have your thread use a free connection from the pool for every concurrent sql transaction.

                implementation is left as an exercise for the OP.

                M Offline
                M Offline
                mvsri
                wrote on last edited by
                #9

                @Kent-Dorfman I will take your suggestions as well as @Christian-Ehrlicher suggestion and implement them.

                1 Reply Last reply
                0
                • M mvsri

                  @Christian-Ehrlicher Thank you for the suggestion I will implement it and check the result.
                  Although before proceeding my query is since I'm reading data from serialthread and emitting it to mainscreen and in mainscreen I'm displaying the values as well as storing the values in the database using databasethread. My point here is should I use a locking mechanism here when handling the data between threads?

                  Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by Christian Ehrlicher
                  #10

                  @mvsri said in Handling Database on Multiple Thread:

                  My point here is should I use a locking mechanism here when handling the data between threads?

                  Don't use threads and you don't need a locking.
                  Use signals and slots properly and you don't need a locking even between threads.

                  https://doc.qt.io/qt-5/signalsandslots.html

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  M 1 Reply Last reply
                  3
                  • Christian EhrlicherC Christian Ehrlicher

                    @mvsri said in Handling Database on Multiple Thread:

                    My point here is should I use a locking mechanism here when handling the data between threads?

                    Don't use threads and you don't need a locking.
                    Use signals and slots properly and you don't need a locking even between threads.

                    https://doc.qt.io/qt-5/signalsandslots.html

                    M Offline
                    M Offline
                    mvsri
                    wrote on last edited by
                    #11

                    @Christian-Ehrlicher I rewrote the code and implemented everything with signals and slots, thank you for the suggestions. It pretty much sums up the questions I had.

                    I just need one suggestion though what is the real use case of blocking serial communication method Qt serial blocking example. When do we use such kind of code then?

                    Christian EhrlicherC 1 Reply Last reply
                    1
                    • M mvsri

                      @Christian-Ehrlicher I rewrote the code and implemented everything with signals and slots, thank you for the suggestions. It pretty much sums up the questions I had.

                      I just need one suggestion though what is the real use case of blocking serial communication method Qt serial blocking example. When do we use such kind of code then?

                      Christian EhrlicherC Online
                      Christian EhrlicherC Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #12

                      @mvsri said in Handling Database on Multiple Thread:

                      When do we use such kind of code then?

                      I would say - mostly never. Maybe if there is really much data coming in from the serial port (e.g. in high-speed modes when you can get ~8MBit/s) to make sure all the data can be handled without killing the gui interaction. But as I said earlier - don't use threads until you really need them :)

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      1 Reply Last reply
                      4
                      • Kent-DorfmanK Offline
                        Kent-DorfmanK Offline
                        Kent-Dorfman
                        wrote on last edited by
                        #13

                        to continue the point @Christian-Ehrlicher mentioned above, blocking serial tasks usually exist on server daemon applications, but not on anything with user interaction or a GUI.

                        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