Properly locking a List in MultiThreaded Scenarios?

前端 未结 5 1708
难免孤独
难免孤独 2020-11-30 08:52

Okay, I just can\'t get my head around multi-threading scenarios properly. Sorry for asking a similar question again, I\'m just seeing many different \"facts\" around the in

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-30 09:16

    The first way is wrong, as each caller will lock on a different object. You could just lock on the list.

    lock(_myList)
    {
       _myList.Add(...)
    }
    

提交回复
热议问题