■ スレッド セーフなコレクション
* From .NET Framework 4 * System.Collections.Concurrent 名前空間が導入 (Concurrent = 同時)
Concurrentに属するコレクション型
【1】BlockingCollection 【2】ConcurrentDictionary 【3】ConcurrentQueue 【4】ConcurrentStack 【5】ConcurrentBag 【6】IProducerConsumerCollection
【2】ConcurrentDictionary
* キーと値のディクショナリのスレッド セーフ版
使いどころ
https://docs.microsoft.com/ja-jp/dotnet/standard/collections/thread-safe/when-to-use-a-thread-safe-collection* 複数のスレッドから同時にキーまたは値を追加および更新する場合に使用 => 読み取りも更新も多いシナリオでは、通常、コンピューターに任意の数のコアを備えられる場合は ConcurrentDictionary<TKey,TValue> の方が大幅に高速VS Dictionary
* キーまたは値の読み取りのみを行う場合、ディクショナリがスレッドによって変更されないのであれば 同期は不要なため、Dictionary<TKey,TValue> の方が高速
【4】ConcurrentStack
* スタックのスレッド セーフ版 => スタック = 後入れ先出し (LIFO: Last In First Out)
【6】IProducerConsumerCollection
* BlockingCollection で使用するために型が実装する必要があるインターフェイス
参考文献
公式サイト
https://docs.microsoft.com/ja-jp/dotnet/standard/collections/thread-safe/https://docs.microsoft.com/ja-jp/dotnet/standard/collections/thread-safe/when-to-use-a-thread-safe-collection
ConcurrentDictionary
https://docs.microsoft.com/ja-jp/dotnet/standard/collections/thread-safe/how-to-add-and-remove-items
ConcurrentBag
https://docs.microsoft.com/ja-jp/dotnet/api/system.collections.concurrent.concurrentbag-1?view=netframework-4.7.2
その他サイト
ConcurrentDictionaryhttps://ufcpp.net/blog/2016/12/tipsconcurrentcollections/
https://qiita.com/Tanutan93337836/items/655c396939c13cd77efb
http://ryuchan.hatenablog.com/entry/2013/09/03/222517
http://engineering.grani.jp/entry/2017/07/28/145035
http://ryuchan.hatenablog.com/entry/2013/09/08/193122
関連記事
C# / コレクション
List (リスト)https://blogs.yahoo.co.jp/dk521123/22295813.html
Dictionary (ディクショナリ)
https://blogs.yahoo.co.jp/dk521123/21978660.html
BlockingCollection ~ Producer-Consumerパターン ~
https://blogs.yahoo.co.jp/dk521123/37943991.html