c# ConcurrentDictionary和ConcurrentQueue怎么使用?

2019-12-23 12:52发布


上图是我的数据结构。我定义了一个线程安全的静态变量(作用:消息队列)用来存储消息,如下:

private static ConcurrentDictionary<string, ConcurrentQueue<SignalModel>> _dic = new ConcurrentDictionary<string, ConcurrentQueue<SignalModel>>();

既用到了ConcurrentDictionary又用到了ConcurrentQueue。用字典是因为要根据IP分组存储。

我就是不知道用了ConcurrentDictionary之后,字典的value是否还有必要使用ConcurrentQueue来定义。

是否应该将ConcurrentQueue改成List?

1条回答
小情绪 Triste *
2楼-- · 2019-12-23 13:09

参考 Concurrent collections is it safe to modify from different threads

ConcurrentQueue is only ensuring that calls to methods of that class are observed to be atomic; it is making no other guarantees surrounding thread safety.

查看更多
登录 后发表回答