The difference between timer and thread

1 software timer

Many students like to use software timers in engineering because they are simple to use. They only need to set a time length and its ontime event. Indeed, the efficiency of software timer is good in some repetitive work with weak persistence, but it also has great disadvantages.

Disadvantage 1: speed: the accuracy of the software timer is relatively low, which is determined by the non real-time characteristics of windows. Under XP, if all processes that can be closed are closed, the software timer of MFC can reach an accuracy of nearly 15ms, while under WIN2000, it can reach an accuracy of nearly 10ms. However, the actual situation is that some processes cannot be closed, such as the database server, so the accuracy of MFC software timer is generally about 40ms, BCB and Delphi are even worse, about 55ms. Query performance counter can greatly improve the accuracy, but the stability is not good.

Disadvantage 2, efficiency: the essence of software timer is actually to process WM in message loop_ Timer message, while WM_ Timer message is a low-level message in message queue, so timer can not fully guarantee the accuracy of processing time interval. In addition, timer occupies the resources of the main thread. It seems parallel, but it is actually serial. Therefore, once the message queue of the form is blocked, the system will fake death or run slowly, which is almost unbearable for the UI.

2 multithreading

Multithreading technology is commonly used in control engineering, because there are a lot of data processing in the closed-loop system. Obviously, these processing can not be processed in the main thread, and most of them are used in threads. The advantage of multithreading is obvious, that is, it throws hard things into the background, and the utilization rate of CPU is relatively high. If the control is good, multithreading has almost no disadvantages, but in fact, there are not many good controls… The reasons are as follows:

1. The time slice is uncontrollable, and the matter of grabbing CPU resources ~ most people can’t tell;

2. Synchronization is complex and prone to deadlock. Generally, the synchronization of three threads can toss people to death. I prefer to use the critical area for synchronization. The reason is also very simple: because the critical area is relatively simple

Multiple threads are multiple tasks executed “at the same time”; Timing can be regarded as a task inserted in this thread.

If multiple tasks need to be executed at the same time, multithreading should be used; If you need to do something at regular intervals, use timing. Multithreading is relatively complex and takes up slightly more system resources, so try not to use multithreading if you can.

When looking at the project code, I see that most of them use timers and sometimes threads. What is the difference between threads and timers?

I read some explanations online:

Timer: when two timers are started in the program, it gives the illusion that they are carried out at the same time…. In fact, it’s naked serial,

Apart from the serial operation of the timer and the efficiency, it is almost as few as threads.

See the operation of the two timers as follows:

QTImer *time=new QTimer;

time-》start(2000);

connect(time,SIGNAL(timeout()),this,SLOT(slot_time1()));

QTimer *time1=new QTimer;

time1-》start(3000);

connect(time1,SIGNAL(timeout()),this,SLOT(slot_time2()));

void MainWindow::slot_ time1()

{

qDebug()《《“——————time1——————-”;

for(int i=0;i《30000;i++)

{

for(int n=0;n《65000;n++)

{

;

}

}

qDebug()《《“——————————————-”;

}

void MainWindow::slot_ time2()

{

qDebug()《《“***********************time2****************”;

for(int i=0;i《60000;i++)

{

for(int n=0;n《65000;n++)

{

;

}

}

qDebug()《《“********************************************”;

}

The difference between timer and thread

When timer 1 is triggered, a delay is added. If it is a thread, the second thread will run immediately, but the timer is not. Other threads will not run until the first thread runs.

Look at threads again:

mythread *th=new mythread;

th-》start();

mythread1 *th1=new mythread1;

th1-》start();

void mythread::run()

{

qDebug()《《“——————time1——————-”;

this-》sleep(2);

qDebug()《《“——————————————-”;

}

void mythread1::run()

{

qDebug()《《“********************time2*************************”;

this-》sleep(4);

qDebug()《《“**********************************************”;

}

The difference between timer and thread

The program starts running thread 2 on the line and delays for 4S. During this period, it does not block, but runs thread 1. After 2S, it returns to thread 2.

Summary:

Therefore, when running several time-consuming functions, try to use threads instead of timers as much as possible, although timers are very simple and easy to maintain.

But the timer does not reduce the running time and memory of the program. It’s just that the code is easier to write and maintain

Leave a Reply

Your email address will not be published. Required fields are marked *