Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linked List Thread safe?

Well i writing in .net and i have a list to witch i will only add item never remove and its a linked list i can change that if its not the best pick but any way to the point would it be safe to not use any locking in this case when i know that this list will never be changed in any other manner but that its added to? (a lock will be used when trying to add to the list)?

like image 868
Peter Avatar asked Jan 24 '23 18:01

Peter


2 Answers

No; to support many readers and one writer (comments to Jared's reply), you might want to look at ReaderWriterLockSlim. The writer requires exclusive access; the readers can co-operate. This is what ReaderWriterLockSlim does. There is also ReaderWriterLock pre 3.5.

You will need to handle enter/exit etc manually - ideally via try/finally.

like image 186
Marc Gravell Avatar answered Jan 31 '23 23:01

Marc Gravell


No it is not safe. LinkedList is not a thread safe class. The only supported multi-thread scenario for LinkedList is multiple readers

http://msdn.microsoft.com/en-us/library/he2s3bh7.aspx

like image 28
JaredPar Avatar answered Feb 01 '23 00:02

JaredPar