List.Add() thread safety

后端 未结 9 2305
庸人自扰
庸人自扰 2020-11-29 07:57

I understand that in general a List is not thread safe, however is there anything wrong with simply adding items into a list if the threads never perform any other operation

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 08:23

    As others already said, you can use concurrent collections from the System.Collections.Concurrent namespace. If you can use one of those, this is preferred.

    But if you really want a list which is just synchronized, you could look at the SynchronizedCollection-Class in System.Collections.Generic.

    Note that you had to include the System.ServiceModel assembly, which is also the reason why I don't like it so much. But sometimes I use it.

提交回复
热议问题