Solved 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:
-
periodically collect hardware usage and then do some simple calculation (cost little time)
-
write the calculated data into data base and log file (cost some time)
-
if has remote setting, also need transfer the data to network (the period may not frequently as collection)
-
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
VScustomer qthread
using woke code inprotected function run
, which is best? -
-
@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?
-
@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