{"id":245727,"date":"2014-12-12T23:35:02","date_gmt":"2014-12-12T19:35:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=245727"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=245727","title":{"rendered":"<span class=\"post_title\">Concurrency \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0432 .net. ConcurrentDictionary \u0438\u0437\u043d\u0443\u0442\u0440\u0438<\/span>"},"content":{"rendered":"<div class=\"content html_format\"> \t\t\t\u0412\u0441\u0435 \u043d\u0430\u0447\u0430\u043b\u043e\u0441\u044c \u0441 \u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043e\u0431\u0435\u0441\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0438 \u043d\u0430\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u043e \u043c\u0435\u043d\u044f \u043a \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044e \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438. \u0414\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u043d\u0430 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435 .Net \u043d\u0435 \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u0442 \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u0432\u0435\u0449\u0438, \u0445\u043e\u0442\u044f \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0438\u0445 \u043f\u043e\u0432\u0441\u0435\u0434\u043d\u0435\u0432\u043d\u043e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 lock-\u043e\u043c \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u044e\u0442 \u0432\u0441\u0435 \u043c\u0435\u0442\u043e\u0434\u044b, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0438\u0435 ConcurrentDictionary, \u0445\u043e\u0442\u044f \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u043e\u0431\u043e\u0439\u0442\u0438\u0441\u044c \u043e\u0431\u044b\u0447\u043d\u044b\u043c Dictionary&lt;&gt;.<\/p>\n<p>  \u0412 \u043d\u0430\u0443\u043a\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 3 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u044b\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0434\u0430\u043d\u043d\u044b\u0445:<br \/>  \u2022 Lock-free \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445;<br \/>  \u2022 Fine-grained \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430;<br \/>  \u2022 Transactional memory implementation(\u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c);<\/p>\n<p>  ConcurrentDictionary&lt;TKey, TValue&gt; \u2014 \u044d\u0442\u043e thread-safe \u0430\u043d\u0430\u043b\u043e\u0433 Dictionary&lt;TKey, TValue&gt;. \u0412 \u0435\u0433\u043e \u043e\u0441\u043d\u043e\u0432\u0435 \u043b\u0435\u0436\u0438\u0442, \u0442\u0430\u043a \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u043c\u044b\u0439 Fine-grained \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430.<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h4>\u041e\u0431\u0449\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f<\/h4>\n<p>  \u0414\u043b\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0435\u0441\u0442\u044c 2 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430: <br \/>  \u2022 <b>\u0441apacity<\/b> \u2014 \u043f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u043a\u043e\u043b-\u0432\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432. \u041f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u2014 31.<br \/>  \u2022 <b>concurrencyLevel<\/b> \u2013 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u043c\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043d\u0430 \u0437\u0430\u043f\u0438\u0441\u044c. \u041f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0432\u044b\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432 =4 * \u0447\u0438\u0441\u043b\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u043e\u0432<\/p>\n<p>  \u041f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u0442\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u0434\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e \u0431\u044b \u043f\u043e\u043d\u044f\u0442\u044c \u0438\u0445 \u0438\u0441\u0442\u0438\u043d\u043d\u0443\u044e \u0441\u0443\u0442\u044c.<\/p>\n<h5>\u0421apacity<\/h5>\n<p>  \u0414\u0430\u043d\u043d\u044b\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u0435\u043d \u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432 \u043e\u0431\u044b\u0447\u043d\u043e\u043c \u00ab\u0441\u043b\u043e\u0432\u0430\u0440\u0435\u00bb Dictionary&lt;TKey, TValue&gt; \u2014 \u044d\u0442\u043e \u043f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0430\u0437\u043c\u0435\u0440 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432. <br \/>  \u0415\u0441\u043b\u0438 \u043c\u044b \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438, \u0442\u043e \u0443\u0432\u0438\u0434\u0438\u043c \u0441\u0442\u0440\u043e\u0447\u043a\u0443:  <\/p>\n<pre><code>ConcurrentDictionary&lt;TKey, TValue&gt;.Node[] buckets = new ConcurrentDictionary&lt;TKey, TValue&gt;.Node[capacity]; <\/code><\/pre>\n<p>  \u041a\u0430\u043a \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e, Dictionary \u2013 \u044d\u0442\u043e \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0445\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0432 .Net \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 GetHashCode() \u0438 \u0438\u043c\u0435\u0435\u0442 \u0442\u0438\u043f int. \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0445\u0435\u0448\u0435\u0439 \u043a\u0430\u043a \u0440\u0430\u0437 \u0438 \u043b\u0435\u0436\u0430\u0442 \u0432 \u0431\u0430\u043a\u0435\u0442\u0430\u0445 \u0432\u044b\u0448\u0435, \u043d\u0443\u0436\u043d\u044b\u0439 \u0431\u0430\u043a\u0435\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442\u0441\u044f:  <\/p>\n<pre><code>bucketNo = (hashcode & int.MaxValue) % bucketCount; <\/code><\/pre>\n<p>  \u041e\u0442\u0441\u044e\u0434\u0430 \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u2014 O(1). <br \/>  \u041f\u0440\u0438 \u043f\u0435\u0440\u0435\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438, \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441 \u0431\u0430\u043a\u0435\u0442\u0430\u043c\u0438 (newLength = oldLength * 2 + 1) \u0438 \u0437\u0430\u043d\u043e\u0432\u043e \u043f\u0435\u0440\u0435\u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0442\u0441\u044f \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b.<\/p>\n<h5>ConcurrencyLevel<\/h5>\n<p>  \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u043c\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043d\u0430 \u0437\u0430\u043f\u0438\u0441\u044c. \u0418\u0437 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0441\u0440\u0430\u0437\u0443 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u0432\u043e\u043f\u0440\u043e\u0441 \u2013 \u0430 \u0437\u0430\u0447\u0435\u043c \u00ab\u0441\u043b\u043e\u0432\u0430\u0440\u044e\u00bb \u0437\u043d\u0430\u0442\u044c \u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432 \u043f\u043e\u0442\u043e\u043a\u043e\u0432. \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0432\u0441\u0435 \u043f\u0440\u043e\u0441\u0442\u043e. \u042d\u0442\u043e \u043d\u0435 \u0447\u0442\u043e \u0438\u043d\u043e\u0435, \u043a\u0430\u043a \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b\u0445 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a (\u043f\u0443\u043b \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a), \u0442.\u0435. \u044d\u0442\u043e \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u00ab\u043f\u0438\u0441\u0430\u0442\u044c\u00bb \u0432 \u00ab\u0441\u043b\u043e\u0432\u0430\u0440\u044c\u00bb \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e. \u041f\u043e \u0441\u0443\u0442\u0438, \u043a\u0430\u0436\u0434\u043e\u0439 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0435 \u00ab\u043e\u0442\u0434\u0430\u0435\u0442\u0441\u044f\u00bb \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u0431\u0430\u043a\u0435\u0442\u043e\u0432, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043e\u043d\u0430 \u0438 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044e \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043d\u0430 \u0437\u0430\u043f\u0438\u0441\u044c.<\/p>\n<h4>\u041e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043d\u0430\u0434 ConcurrentDictionary.<\/h4>\n<p>  \u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043d\u0430\u0434 \u0441\u043b\u043e\u0432\u0430\u0440\u0435\u043c \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043d\u0430 3 \u0433\u0440\u0443\u043f\u043f\u044b:   <\/p>\n<ul>\n<li>\u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u043d\u0435\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u0435\u043c\u044b\u0435;<\/li>\n<li>\u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u043e\u0434\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438\u0437 \u043f\u0443\u043b\u0430 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a;<\/li>\n<li>\u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u0441\u0435\u0433\u043e \u0441\u043b\u043e\u0432\u0430\u0440\u044f;<\/li>\n<\/ul>\n<p>  <i>\u041a \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u043d\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u0435\u043c\u044b\u043c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u043c \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u043d\u0435\u0441\u0442\u0438:<\/i>  <\/p>\n<ul>\n<li>ContainsKey<\/li>\n<li>TryGet<\/li>\n<li>this [ ]<\/li>\n<li>GetEnumerator \u2013 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043d\u0435 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u0446\u0435\u043b\u043e\u0441\u0442\u043d\u043e\u0441\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0445 (\u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0441\u043d\u0435\u043f\u0448\u043e\u0442\u044b), \u0442.\u0435. \u0434\u0430\u043d\u043d\u044b\u0435 \u0437\u0430 \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c\u0441\u044f.<\/li>\n<\/ul>\n<p>  \u0412\u0441\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0447\u0442\u0435\u043d\u0438\u044f (Get\/ContainsKey) \u0438\u043c\u0435\u044e\u0442 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0440\u0430\u0431\u043e\u0442\u044b:  <\/p>\n<ul>\n<li>\u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0445\u0435\u0448\u0430 \u043a\u043b\u044e\u0447\u0430 \u0447\u0435\u0440\u0435\u0437 GetHashCode()<\/li>\n<li>\u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0431\u0430\u043a\u0435\u0442\u0430, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043b\u0435\u0436\u0438\u0442 \u043d\u0430\u0448 \u044d\u043b\u0435\u043c\u0435\u043d\u0442<\/li>\n<li>\u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043a\u043b\u044e\u0447\u0430 \u0432 \u0431\u0430\u043a\u0435\u0442\u0435 \u0441 \u0442\u0435\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443 \u043d\u0430\u0441<\/li>\n<li>\u0447\u0442\u0435\u043d\u0438\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c Volatile.Read<\/li>\n<\/ul>\n<p>  <i>\u041a \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u043c \u0441 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u043e\u0439 \u043e\u0434\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438\u0437 \u043f\u0443\u043b\u0430 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u043d\u0435\u0441\u0442\u0438:<\/i>  <\/p>\n<ul>\n<li>TryAdd<\/li>\n<li>TryUpdate<\/li>\n<li>TryRemove<\/li>\n<\/ul>\n<p>  \u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0440\u0430\u0431\u043e\u0442\u044b:  <\/p>\n<ol>\n<li>\u0412\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0445\u0435\u0448\u0430 \u043a\u043b\u044e\u0447\u0430 \u043d\u043e\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430<\/li>\n<li>\u0412\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0431\u0430\u043a\u0435\u0442\u0430 bucketNo, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u0438 \u043d\u043e\u043c\u0435\u0440\u0430 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 \u0438\u0437 \u043f\u0443\u043b\u0430<br \/> \n<pre><code>bucketNo = (hashcode & int.MaxValue) % bucketCount; lockNo = bucketNo % lockCount; <\/code><\/pre>\n<p>  <\/li>\n<li>\u0411\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 bucketNo \u0447\u0435\u0440\u0435\u0437 Monitor.Enter<\/li>\n<li>\u0417\u0430\u043f\u0438\u0441\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c Volatile.Write<\/li>\n<li>\u041e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 Monitor.Exit<\/li>\n<\/ol>\n<p>  <i>\u041a \u0441\u0430\u043c\u044b\u043c \u043d\u0435\u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u043c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0442 \u0432\u0435\u0441\u044c \u0441\u043b\u043e\u0432\u0430\u0440\u044c, \u043e\u0442\u043d\u043e\u0441\u044f\u0442\u0441\u044f:<\/i>  <\/p>\n<ul>\n<li>Count, IsEmpty. \u0414\u0430, \u044d\u0442\u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0442\u0440\u0435\u0431\u0443\u044e\u0442 \u043f\u043e\u043b\u043d\u043e\u0439 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 \u0441\u043b\u043e\u0432\u0430\u0440\u044f. \u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432 \u043b\u043e\u0433-\u0444\u0430\u0439\u043b \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c GetEnumerator \u0438 LINQ.<\/li>\n<li>Keys, Values \u2013 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 \u043a\u043b\u044e\u0447\u0435\u0439 \u0438 \u0441\u043f\u0438\u0441\u043a\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e. \u041a\u0441\u0442\u0430\u0442\u0438, \u0442\u0443\u0442 \u043f\u043e\u043b\u0443\u0447\u0430\u044e\u0442\u0441\u044f \u0446\u0435\u043b\u043e\u0441\u0442\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u2013 \u0441\u043d\u0435\u043f\u0448\u043e\u0442\u044b.<\/li>\n<li>CopyTo \u2013 explicit ICollection<\/li>\n<li>Clear, ToArray <\/li>\n<\/ul>\n<h5>\u041c\u0435\u0442\u043e\u0434\u044b AddOrUpdate, GetOrAdd<\/h5>\n<p>  \u042d\u0442\u0438 \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b \u0442\u0435\u043c, \u0447\u0442\u043e \u043e\u043d\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 Add\/Get\/Update, \u043d\u043e \u0441\u0430\u043c\u0438 \u043d\u0435 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u043c\u0438, \u043e\u043d\u0438 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443 \u043d\u0430 \u0432\u0441\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e. \u0412\u043e\u0442 \u0442\u0430\u043a \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f AddOrUpdate:  <\/p>\n<pre><code>      do       {         while (!TryGetValue(key, out comparisonValue))         {           TValue obj = addValueFactory(key);           TValue resultingValue;           if (TryAddInternal(key, obj, false, true, out resultingValue))             return resultingValue;         }         newValue = updateValueFactory(key, comparisonValue);       }       while (!TryUpdate(key, newValue, comparisonValue));       return newValue; <\/code><\/pre>\n<p>  MSDN:  <\/p>\n<blockquote><p>Also, although all methods of ConcurrentDictionary are thread-safe, not all methods are atomic, specifically GetOrAdd and AddOrUpdate. The user delegate that is passed to these methods is invoked outside of the dictionary&#8217;s internal lock. (This is done to prevent unknown code from blocking all threads.)  <\/p><\/blockquote>\n<p>  \u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f:  <\/p>\n<pre><code> items.AddOrUpdate(srcKey, srcValue,                 (key, existingVal) =&gt;                 {                     \/\/ \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u043c \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0441\u043b\u043e\u0432\u0430\u0440\u044f                     if (srcValue != existingVal)                         throw new ArgumentException(&quot;...&quot;);   <\/code><\/pre>\n<h5>\u0412 \u043a\u043e\u043d\u0446\u0435, \u0445\u043e\u0447\u0443 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043c\u0435\u0442\u043e\u0434\u043e\u0432:<\/h5>\n<p>   <\/p>\n<ol>\n<li>TryAdd, TryUpdate, TryRemove, \u2014 O(1)<\/li>\n<li>Get\/Contains, Item[] \u043f\u043e \u043a\u043b\u044e\u0447\u0443 \u2014 O(1)<\/li>\n<li>ContainsValue, ToArray(), Keys, Values \u2014 O(n)<\/li>\n<\/ol>\n<p>  P.s. \u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0445\u043e\u0442\u0435\u043b \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0442\u043e\u043d\u043a\u043e\u0441\u0442\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f ConcurrentDictionary \u0438 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043c\u043e\u043c\u0435\u043d\u0442\u044b \u0432 \u0435\u0433\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438. \t\t\t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/245727\/\"> http:\/\/habrahabr.ru\/post\/245727\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\"> \t\t\t\u0412\u0441\u0435 \u043d\u0430\u0447\u0430\u043b\u043e\u0441\u044c \u0441 \u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043e\u0431\u0435\u0441\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0438 \u043d\u0430\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u043e \u043c\u0435\u043d\u044f \u043a \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044e \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438. \u0414\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u043d\u0430 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435 .Net \u043d\u0435 \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u0442 \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u0432\u0435\u0449\u0438, \u0445\u043e\u0442\u044f \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0438\u0445 \u043f\u043e\u0432\u0441\u0435\u0434\u043d\u0435\u0432\u043d\u043e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 lock-\u043e\u043c \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u044e\u0442 \u0432\u0441\u0435 \u043c\u0435\u0442\u043e\u0434\u044b, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0438\u0435 ConcurrentDictionary, \u0445\u043e\u0442\u044f \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u043e\u0431\u043e\u0439\u0442\u0438\u0441\u044c \u043e\u0431\u044b\u0447\u043d\u044b\u043c Dictionary&lt;&gt;.<\/p>\n<p>  \u0412 \u043d\u0430\u0443\u043a\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 3 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u044b\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0434\u0430\u043d\u043d\u044b\u0445:<br \/>  \u2022 Lock-free \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445;<br \/>  \u2022 Fine-grained \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430;<br \/>  \u2022 Transactional memory implementation(\u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c);<\/p>\n<p>  ConcurrentDictionary&lt;TKey, TValue&gt; \u2014 \u044d\u0442\u043e thread-safe \u0430\u043d\u0430\u043b\u043e\u0433 Dictionary&lt;TKey, TValue&gt;. \u0412 \u0435\u0433\u043e \u043e\u0441\u043d\u043e\u0432\u0435 \u043b\u0435\u0436\u0438\u0442, \u0442\u0430\u043a \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u043c\u044b\u0439 Fine-grained \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-245727","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/245727","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=245727"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/245727\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=245727"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=245727"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=245727"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}