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. QTimer VS Qthread in console application?
Forum Update on Monday, May 27th 2025

QTimer VS Qthread in console application?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 2.2k 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.
  • S Offline
    S Offline
    SpartaWHY117
    wrote on last edited by
    #1

    when I first ask almost same question a year ago in GUI program, most answer told me timer is enough. But when comes to console application, timer seems not enough.

    For now, my program has four function:

    1. periodically collect hardware usage and then do some simple calculation (cost little time)

    2. write the calculated data into data base and log file (cost some time)

    3. if has remote setting, also need transfer the data to network (the period may not frequently as collection)

    4. show the calculated the data in terminal (output stdout)

    however, when comes to console application, the design pattern of achieving these four function get me there. It seems that a single thread and timers can't be enough. Actually, because only function 1 need to write data, other function is read data, it seems like multithread is not so complex for this project.

    my current plan is using thread 1 to do function 1 , and thread 2 to do function 2,4. then function 3 done in thread 3. Does this design reasonable?

    And if using qthread, about funcion 1 .Has a member qthread VS customer qthread using woke code in protected function run , which is best?

    jsulmJ 1 Reply Last reply
    0
    • S SpartaWHY117

      when I first ask almost same question a year ago in GUI program, most answer told me timer is enough. But when comes to console application, timer seems not enough.

      For now, my program has four function:

      1. periodically collect hardware usage and then do some simple calculation (cost little time)

      2. write the calculated data into data base and log file (cost some time)

      3. if has remote setting, also need transfer the data to network (the period may not frequently as collection)

      4. show the calculated the data in terminal (output stdout)

      however, when comes to console application, the design pattern of achieving these four function get me there. It seems that a single thread and timers can't be enough. Actually, because only function 1 need to write data, other function is read data, it seems like multithread is not so complex for this project.

      my current plan is using thread 1 to do function 1 , and thread 2 to do function 2,4. then function 3 done in thread 3. Does this design reasonable?

      And if using qthread, about funcion 1 .Has a member qthread VS customer qthread using woke code in protected function run , which is best?

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

      @SpartaWHY117 said in QTimer VS Qthread in console application?:

      It seems that a single thread and timers can't be enough

      Why not? How do you know?

      "And if using qthread, about funcion 1 .Has a member qthread VS customer qthread using woke code in protected function run , which is best?" - I don't understand what you mean.

      If you really want to use threads then you really should read https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/ first.

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

      S 1 Reply Last reply
      0
      • jsulmJ jsulm

        @SpartaWHY117 said in QTimer VS Qthread in console application?:

        It seems that a single thread and timers can't be enough

        Why not? How do you know?

        "And if using qthread, about funcion 1 .Has a member qthread VS customer qthread using woke code in protected function run , which is best?" - I don't understand what you mean.

        If you really want to use threads then you really should read https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/ first.

        S Offline
        S Offline
        SpartaWHY117
        wrote on last edited by
        #3

        @jsulm if use single thread ,and connect the timers slots in this thread, the slots are execute in sequential. what i really concerned is that if some slots do the costing time work, will it affect the timer which used for collecting hardware usage every second?

        jsulmJ J.HilkJ 2 Replies Last reply
        0
        • S SpartaWHY117

          @jsulm if use single thread ,and connect the timers slots in this thread, the slots are execute in sequential. what i really concerned is that if some slots do the costing time work, will it affect the timer which used for collecting hardware usage every second?

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

          @SpartaWHY117 From your description I only see need for one timer. For what do you want to have more of them?
          Getting the info once per second and writing it to db/file/console should take much less than one second.

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

          1 Reply Last reply
          0
          • S SpartaWHY117

            @jsulm if use single thread ,and connect the timers slots in this thread, the slots are execute in sequential. what i really concerned is that if some slots do the costing time work, will it affect the timer which used for collecting hardware usage every second?

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @SpartaWHY117 You should probably look into QtConcurrent its more or less intendet to run single heavy duty functions parallell to your main thread.

            You can run the same function multiple times, as long as you're consider possible read/write errors on member variables.


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            S 1 Reply Last reply
            1
            • J.HilkJ J.Hilk

              @SpartaWHY117 You should probably look into QtConcurrent its more or less intendet to run single heavy duty functions parallell to your main thread.

              You can run the same function multiple times, as long as you're consider possible read/write errors on member variables.

              S Offline
              S Offline
              SpartaWHY117
              wrote on last edited by
              #6

              @J.Hilk Qt Concurrent seems really like to std::async and wrapped thread into threadpool

              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