Is the List.AddRange() thread safe?

前端 未结 5 1693
天命终不由人
天命终不由人 2021-02-07 09:14

Can I, without locking, safely call List.AddRange(r) from multiple threads? If not, what sort of trouble would I run into?

5条回答
  •  醉梦人生
    2021-02-07 10:02

    No it is not thread-safe.

    Thread A could call AddRange on your list. It could iterate partially over the collection and switch threads.

    Thread B could call Add/Remove, etc. before Thread A has finished.

提交回复
热议问题