Does CLR internally spawns a thread to respond to timer events?

被刻印的时光 ゝ 提交于 2019-11-28 06:35:51

问题


Hans and I had small discussion recently about the subject and I'm curious how it is really implemented.

See initial talking in the comments here: Are c# timers naturally multithreaded?


回答1:


Staring at the .Net 4.0 reference source, System.Timers.Timer seems to use a System.Threading.Timer to handle the actual implementation details. The latter generates timers by calling AddTimerNative. AddTimerNative is an internal call.

Googling AddTimerNative lead to a lot of interesting results, including Willy Denoyette's comment, where he states that it calls CreateTimerQueueTimer.

That page states that "Callback functions are queued to the thread pool. "




回答2:


Well, I did some investigations... Details are in my blog post

To be short, just summary from there:

  • As for SSCLI20 Hans was totally right. There’s really separate thread for handing APCs and queued timers. Thanks for your insistence, btw. I received a chance to dig something interesting :)
  • On newer systems it still could be implemented without additional threads. I was just mistakenly assuming it was already there when CLR 2.0 was written. For details see CreateThreadpoolTimer, SetThreadpoolTimer and CloseThreadpoolTimer.


来源:https://stackoverflow.com/questions/6343733/does-clr-internally-spawns-a-thread-to-respond-to-timer-events

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!