Need a Syncroot for a LinkedList(of T)

夙愿已清 提交于 2019-12-23 05:14:33

问题


I am using VB.Net and would like to use a LinkedList. Only problem is that it is a multithreaded application. I saw from MSDN that Syncroot is an explicit implementation of the ICollection Interface. I found people wanting to do similar things with List(Of T). It seems that the solution there is to cast the list to the interface.

I have tried to do what I would imagine to be a similar thing in VB.Net, basically:

Dim TestLinkedList = New LinkedList(Of Long)
SyncLock (Ctype(TestLinkedList, ICollection)).SyncRoot
    .
    .
    .
End SyncLock

Is the above correct?


回答1:


It will work, that's about all that can be said for it. SyncRoot was a mistake from .NET 1.1, there's no reason to continue the practice.

Dim list = New LinkedList(Of Long)
Dim listLock = New Object
...

SyncLock(listLock)
...
End SyncLock



回答2:


ICollection.SyncRoot is generally considered a bad idea. You should either lock the collection itself, or lock on a separate lock object you create



来源:https://stackoverflow.com/questions/2227833/need-a-syncroot-for-a-linkedlistof-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!