473,396 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Synchronised Generic List

Hi,

I am considering using a Generic List to hold user defined class entries.
Since this List will be accessed by multiple threads I need it to be
threadsafe. It appears there is no easy way to do this as there is no
synchronised method for the list.

I suppose I could lock the List whenever I access it but I am concerned this
will affect performance as the list will be accessed frequently.

Can anyone suggest how i can accomplish this syncronisation?

Thanks
Macca
May 30 '06 #1
1 7864
Macca <Ma***@discussions.microsoft.com> wrote:
I am considering using a Generic List to hold user defined class entries.
Since this List will be accessed by multiple threads I need it to be
threadsafe. It appears there is no easy way to do this as there is no
synchronised method for the list.

I suppose I could lock the List whenever I access it but I am concerned this
will affect performance as the list will be accessed frequently.


A synchronized List<T> would perform in the same way as a list that you
lock yourself, since it would be implemented in this way. I.e.
ArrayList.Synchronized doesn't return a lock-free list - it simply
returns a list which locks the inner list during every call.

If the list won't be modified very often but will be accessed extremely
often (and you've measure performance and know that contention is the
reason for poor performance), you could consider using copy / modify /
atomic replace pattern. I.e. make a copy of the list whenever you're
about to change it, change the copy, and then use
Interlocked.CompareExchange to perform a swap, and repeat the copy /
change operation if the reference has changed in the meantime. If you
don't want to deal with the subtleties of things like memory read and
write barriers etc., that probably won't be the way to go.

-- Barry

--
http://barrkel.blogspot.com/
May 30 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Abhi | last post by:
In the following hypothetical example I want to build a generic list of unique string items. How should I implement the pred function so that it returns true/false if target string exists in the...
0
by: Jason | last post by:
I want to create a synchronised queue class which has very simple functionality as a queue but is thread safe, where two different threads can add and remove items in such a way that doesn't...
8
by: JAL | last post by:
Here is my first attempt at a deterministic collection using Generics, apologies for C#. I will try to convert to C++/cli. using System; using System.Collections.Generic; using System.Text; ...
0
by: Wiktor Zychla [C# MVP] | last post by:
We do have generic classes, methods and delegates. My question is: what reason prevents us from having generic properties and indexers? // impossible public List<T> GetList<T> { get { ... }
0
by: crazyone | last post by:
I've got a gaming framework i'm building and i want to save myself the trouble of reading and writting the complete game data to a custom file and load/save it to an XML file but i'm getting...
3
by: Seth Gecko | last post by:
Hi I am working with generic lists of various objects and a control dealing with these lists. For instance: A parent form holds: dim Walls as List(Of wall) dim Segments as List(Of segment) ...
4
by: =?Utf-8?B?QkogU2FmZGll?= | last post by:
We have a class that has a public property that is of type List<T>. FXCop generates a DoNotExposeGenericLists error, indicating "System.Collections.Generic.List<Tis a generic collection designed...
1
by: Suds | last post by:
Hi, I'm having an issue with invoking a Generic method that takes Generic Arguments. My method signature is public void GenericMethodWithGenericArguments<E, V>(List<EtheFirstList,...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.