C#中 non thread-safe是如何导致 high CPU usage?

freecodex 2015-02-02 11:15:16
Mark 在博客中有一篇关于用WinDGB来调试某个进程导致CPU高占的文章。在最后Mark这样描述>>
Doing this for the other four threads yields the same result - all of them are trying to access the cache value belonging to the PartnerID value of 230!
At this point I can quite confidently say that I’m sure this is a threading issue related to the non thread-safe Dictionary usage...
他的结论是:CPU高占是由于有4个线程要访问同一个缓存变量。
但是作者并没有说背后的原因,请问下背后的机制,为什么多个线程访问同一个变量会导致CPU高占呢?我的理解是不一定的.
...全文
132 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
freecodex 2015-02-03
  • 打赏
  • 举报
回复
引用 1 楼 Forty2 的回复:
[quote=引用 楼主 u010024618 的回复:] ...为什么多个线程访问同一个变量会导致CPU高占呢?我的理解是不一定的.
是不一定的,这取决于‘访问’具体作了些什么。 另外,原文所指的问题并不是访问一个变量,而是‘访问一个缓存项’,而且那个访问可能会创建项并缓存(原文注释是If it fails we'll populate the cache and return it.)。 没有线程安全保证下,多线程创建项可能导致数据损坏或死锁;多线程写缓存也可能导致Dictionary内部数据被破坏。 而当数据损坏的时候,灾难是不可预测的。举个例子,比如Dictionary内部用了很多的类似代码: for (int i = buckets[hashCode % buckets.Length]; i >= 0; i = entries[i].next) { ... } 如果entries[i].next不幸地指向它自己,那么该for循环就是一个死循环,导致CPU高就不奇怪了。 [/quote] 多谢,接着这个问题还请教您一个问题,就是如何如何写线程安全的代码?就您的经验来看,有没有一些通用的规则呢?
Forty2 2015-02-03
  • 打赏
  • 举报
回复
引用 楼主 u010024618 的回复:
...为什么多个线程访问同一个变量会导致CPU高占呢?我的理解是不一定的.
是不一定的,这取决于‘访问’具体作了些什么。 另外,原文所指的问题并不是访问一个变量,而是‘访问一个缓存项’,而且那个访问可能会创建项并缓存(原文注释是If it fails we'll populate the cache and return it.)。 没有线程安全保证下,多线程创建项可能导致数据损坏或死锁;多线程写缓存也可能导致Dictionary内部数据被破坏。 而当数据损坏的时候,灾难是不可预测的。举个例子,比如Dictionary内部用了很多的类似代码: for (int i = buckets[hashCode % buckets.Length]; i >= 0; i = entries[i].next) { ... } 如果entries[i].next不幸地指向它自己,那么该for循环就是一个死循环,导致CPU高就不奇怪了。
Twitter Digg Facebook Del.icio.us Reddit Stumbleupon Newsvine Technorati Mr. Wong Yahoo! Google Windows Live Send as Email Add to your CodeProject bookmarks Discuss this article 85 Print Article Database » Database » Other databasesLicence CPOL First Posted 19 Jan 2012 Views 24,219 Downloads 992 Bookmarked 74 times RaptorDB - The Key Value Store V2 By Mehdi Gholam | 8 Mar 2012 | Unedited contribution C#.NETDBABeginnerIntermediateAdvanceddatabase Even faster Key/Value store nosql embedded database engine utilizing the new MGIndex data structure with MurMur2 Hashing and WAH Bitmap indexes for duplicates. See Also More like this More by this author Article Browse Code Stats Revisions (8) Alternatives 4.95 (56 votes) 1 2 3 4 5 4.95/5 - 56 votes μ 4.95, σa 1.05 [?] Is your email address OK? You are signed up for our newsletters but your email address is either unconfirmed, or has not been reconfirmed in a long time. Please click here to have a confirmation email sent so we can confirm your email address and start sending you newsletters again. Alternatively, you can update your subscriptions. Add your own alternative version Introduction What is RaptorDB? Features Why another data structure? The problem with a b+tree Requirements of a good index structure The MGIndex Page Splits Interesting side effects of MGIndex The road not taken / the road taken and doubled back! Performance Tests Comparing B+tree and MGIndex Really big data sets! Index parameter tuning Performance Tests - v2.3 Using the Code Differences to v1 Using RaptorDBString and RaptorDBGuid Global parameters RaptorDB interface Non-clean shutdowns Removing Keys Unit tests File Formats File Format : *.mgdat File Format : *.mgbmp File Format : *.mgidx File Format : *.mgbmr , *.mgrec History Download RaptorDB_v2.0.zip - 38.7 KB Download RaptorDB_v2.1.zip - 39 KB Download RaptorDB_v2.2.zip - 39 KB Download RaptorDB_v2.3.zip - 39.6 KB D

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧