FredrikJohansson
Fri Mar 23 09:06:10 CDT 2007
Don't reinvent the wheel... Use the static member 'Synchronized' to get a
thread-safe collection instance. For instance:
Queue myQueue = Queue.Synchronized(new Queue());
Regards,
Fredrik
"Jon Skeet [C# MVP]" wrote:
> Peter Duniho <NpOeStPeAdM@nnowslpianmk.com> wrote:
>
> <snip>
>
> > You can safely read from multiple threads simultaneously, but you will run
> > into the problem that readers still need to lock the data somehow, so that
> > the data doesn't get modified during reads.
>
> While it's not *dangerous* to read from multiple threads, even if
> you're reading a single int (which is guaranteed to be changed
> atomically) you need to lock, use a memory barrier, or use a volatile
> variable to make sure that the read actually sees the most recent
> value, rather than a potentially previously read one.
>
> See
http://pobox.com/~skeet/csharp/threads/volatility.shtml
>
> (Unfortunately there are various ways in which the .NET memory model is
> very poorly specified, but such is life.)
>
> --
> Jon Skeet - <skeet@pobox.com>
>
http://www.pobox.com/~skeet Blog:
http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too
>