简体   繁体   中英

How can I work with threadsafe Lists in C#?

So, this question asks how to threadsave lists. To sum it up, instead of using

List<int[]> listName = new List<int[]>();

//you use:

SynchronizedCollection<int[]> listName = new SynchronizedCollection<int[]>();

But my problem is, that if I use:

int[] returnArray = new int[listName[0].Length];

I can't access those elements with indexing. How can I access them then? Is there not a way to make lists threadsafe without loosing any functionality?

Maybe I can work with limited functionality, I only need 4 things: reading the lowest and the highest elements (only those), deleting listName[0] and adding something on top. So I don't need any of the elements in the middle actually. But there are 2 threads and depending on what the thread did it will either add a new one on top and take it or delete the lowest one and take the new lowest one.

I can't use queues or stacks because of this, so I am all out of ideas.

Now the question is, is there something that fills those requirements?

您可以使用从多个线程访问对象

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM