List concurrent removing and adding

前端 未结 5 1129
渐次进展
渐次进展 2020-12-17 16:36

I am not too sure, so i thought i\'d ask. Would removing and adding items to a System.Collections.Generic.List<> object be non-thread safe?

My sit

5条回答
  •  悲&欢浪女
    2020-12-17 17:12

    Yes, adding and removing items from a List<> is not thread safe, so you need to synchronise the access, for example using lock.

    Mind that the lock keyword in no ways locks the object that you use as identifier, it only prevents two threads to enter the same code block at the same time. You will need locks around all code that accesses the list, using the same object as identifier.

提交回复
热议问题