Program English --- Timer VS Thread ( from codegurn )

来源:互联网 发布:火箭联盟 知乎 编辑:程序博客网 时间:2024/06/05 19:02

Ask:

Can any one tell me what is the difference between a Timer and  a thread?

Answer:

To put it simply, a thread is a basic unit of execution as far as the Operation System scheduler goes,Consider each thread as operation on it's own set of  tasks. If you were to consider,say you being the OS, and you have 10 different tasks to carry out, you could think of those tasks as different threads. You can then, as an OS, ask one of the  task to start operation, at some later time, you would ask that to stop and ask some other task to resume operation and so on.

A timer is an event. It is something that triggers at a certain time.

This is a very simplistic view.. You need to ask specific answers to go into further details.

Answer:

Two threads are two parts of a program running "simultaneously". If you have only one processor, the time of processor is shared among all the running thread of the computer ans the system tasks. The switch between all these thread/tasks is so fast that it gives the illusion all run simultaneously(e.g. open the clock,while you do something else you see the clock ticking while you can move the mouse and run something else). It is maybe where your "time" intervenes, at the system level there is actually a timer which allows the task manager to switch between tasks - note that this timer is now embedded in processors; the task  switching is done at the processors level,which is a more efficient way of task management.

At the developper level - you - a timer is something you can setup in your program to get triggered after a given time toexecute a given function (e.g. see CWnd::SetTimer() in VC++). Note that, in Windows, when the timer elapses a "timer event" is generated and sent to (for CWnd) the window which created it; if a timer handler has been defined for htis window, it is triggered when Windows can (ie when your program is running the "event loop"; e.g. if your program is performing a long caculation, for instance, which lasts 10 seconds, then entering the event loop, the timer event has to wait for these 10 seconds...). This means that this timer event is triggered "in sequence", and the timer handler does not run sumultaneously with another part if your program.

However, you may instantiate a window object in another thread and setup a timer in this window. In thsi case, the timer event will be triggered from this different thread; if the thread was idle, its handler will start to run quickly (even if  the 10 secs calculation from the other main thread is still running).

I tried to maks a link between timer and threads, and hope it is understanable.

Answer:

There is a very important thing to remember about timers.

A thread will pretty much execute no matter what. Every so often, is will get a timeslice, and do some work.

But when a timer goes off, it is supposed to put a WM_TIMER message in the queue, This is tricky part. The WM_TIMER message only goes in the message queue, if your program is not busy.

So if you are doing something as simple as resizing the window of your program, the WM_TIMER message will not get issued until you are finished.

So only use timers if the timing is not important.