Differences and connections between timers and multithreading

1 Software timer
Many students like to use software timer in engineering, because it is easy to use, only need to set a duration and its OnTime event to use. It is true that software timers are efficient in some repetitive tasks that are not persistent, but they also have 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 close to 15ms, while Under Win2000, it can achieve an accuracy close to 10ms. But the actual situation is that some processes cannot be closed, such as database servers, so the accuracy that MFC's software timer can achieve is generally around 40ms, and BCB and delphi are even worse, about 55ms. QueryPerformanceCounter can greatly improve the accuracy, but the stability is not good.
Disadvantage 2, efficiency: The essence of the software timer is to process the WM_TIMER message in the message loop, and the WM_TIMER message is a low-level message in the message queue, so the timer cannot fully guarantee the accuracy of the processing time interval. In addition, Timer occupies the resources of the main thread, which seems to be parallel but is actually serial, so once the message queue of the form is blocked, it will cause the system to freeze or run slowly, which is almost unbearable for the UI.
2
Multithreading Multithreading technology is a commonly used technology in control engineering, because there is a large amount of data processing in the closed-loop system, these processing obviously cannot be processed in the main thread, and most of them are used in the thread. The advantages of multithreading are obvious, that is, throwing the hard things to the background, and the utilization of the CPU is relatively high. If the control is good, multi-threading has almost no disadvantages, but in fact, the control is not very good... The reasons are as follows:
1. The time slice is uncontrollable, and the thing about grabbing CPU resources ~ ordinary people can't tell;
2. Synchronization It is more complicated and prone to deadlocks, and the synchronization of three threads can generally kill people. Synchronization I prefer to use critical sections, and the reason is very simple: because critical sections are simpler...


Multiple threads are multiple tasks executing "simultaneously"; timing can be thought of as tasks inserted within the thread.
If multiple tasks need to be executed at the same time, use multithreading; if you need to do something at regular intervals, use timing. Multi-threading is relatively complicated and occupies slightly more system resources, so try not to use multi-threading if you can.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326629835&siteId=291194637