List.Add() thread safety

后端 未结 9 2308
庸人自扰
庸人自扰 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条回答
  •  不知归路
    2020-11-29 08:18

    This would cause problems, as the List is built over an array and is not thread safe you might get index out of bounds exception or some values overriding other values, depending on where the threads are. Basically, don't do it.

    There are multiple potential problem... Just don't. If you need a thread safe collection, either use a lock or one of the System.Collections.Concurrent collections.

提交回复
热议问题