List.Add() thread safety

后端 未结 9 2264
庸人自扰
庸人自扰 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:20

    You current approach is not thread-safe - I would suggest avoiding this altogether - since you basically do a data transformation PLINQ might be a better approach ( I know this is a simplified example but in the end you are projecting each transaction into another "state" object).

    List list = transactions.AsParallel()
                                    .Select( tran => new object())
                                    .ToList();
    
        

    提交回复
    热议问题