QTimer VS Qthread in console application?



  • 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?


  • Moderators

    @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.



  • @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?


  • Moderators

    @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.



  • @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.



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.