c# - Multithread adding nodes to LinkedList? -



c# - Multithread adding nodes to LinkedList? -

for illustration have var linkedlist = new linkedlist<int>();

then add together 1 node. linkedlist.addlast(1);

after have 2 threads respectively calling linkedlist.addlast(2); (same statement).

so can safely expect becomes 1->2->2 linked list?

or can become 1->2 when race status happens?

(maybe has visibility issue, before firstly wonder if such race status can happen.)

this question can answered quick little test app. reply 1->2 happen sometimes.

private static void runtest() { (int = 0; < 100; i++) { var lst = new linkedlist<int>(); parallel.for(1, 51, j => { lst.addlast(j); }); if (lst.count < 50) { console.writeline(lst.count); } } }

as has been mentioned, collection not thread-safe. need serialize access or utilize built-in thread-safe collection.

c# .net

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -