473,385 Members | 1,400 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,385 software developers and data experts.

System.Collection.Generic.Queue - Where did SyncRoot go?

I'm not sure if I've just lost it but, I no longer see the property SyncRoot
on a Queue. I see that it's a member of the ICollection Base Object, but it's
not exposed.

Was this intended? Besides writing a wrapper class for Queue, what is the
intended way to lock the queue to enforce proper synchronization?

using System;
using System.Collections.Generic;
using System.Text;

namespace DummyApp
{
class DummyApp
{
static void Main(string[] args)
{
Queue<intiq = new Queue<int>();

lock(q.SyncRoot)
{
DoSomething();
}
}
}
}

This is the error I would get when compiling

'System.Collections.Generic.Queue<int>' does not contain a definition for
'SyncRoot'
Feb 28 '07 #1
2 6075
I believe the view taken was that in most cases the SyncRoot approach
simply wasn't worth it; and synchronising *inside* the object (for
most collections) is worse, as it only protects individual objects -
not longer atomic operations. Note that if you cast to ICollection,
the SyncRoot is still there, and looking at reflector it will allocate
a new sync object when first requested (even though it reports
IsSynchronized as false).

However; you only need to worry about the SyncRoot if you are
threading... and if you are threading you have enough complexity
already... I would tend to encapsulate the Queue<Tinto a class that
describes what I am trying to do, rather than how I am implementing
it; since I recognise that this class is threaded, I can make it
thread-safe with a private lock inside the object, as well as
encapsulating other features such as Monitor.Wait (if reading empty)
and Monitor.Pulse (if writing first entry) - assuming of course that
this is a work queue (which it sounds like).

Marc

Mar 1 '07 #2
only protects individual objects -
not longer atomic operations.
I meant "individual method calls"

Mar 1 '07 #3

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

Similar topics

3
by: wesley | last post by:
Hi List, I have a question on collection synchronisation. For example I have an ArrayList called myList and the code is like this: lock (myList.SyncRoot) { // do update on myList...
4
by: chrisben | last post by:
Hi I often use Queue.Synchronized method to create a queue for multithread writing. I also know I could use SyncRoot and lock to write Queue. Could anyone here please explain to me the pros and...
2
by: Howard Swope | last post by:
Could someone help explain thread safety issues in the System.Collections classes? The documentation states:...
6
by: Paul Delhanty | last post by:
Hi, I am converting an existing native C++ program making heavy use of STL to C#2.0 with STL usage replaced by the new generic collections. The conversion has gone well to the point where I am...
4
by: nhmark64 | last post by:
Hi, Does System.Collections.Generic.Queue not have a Synchronized method because it is already in effect synchronized, or is the Synchronized functionality missing from...
6
by: Arthur Dent | last post by:
How do you sort a generic collection derived from System.Collections.ObjectModel.Collection? Thanks in advance, - Arthur Dent
28
by: Michael Primeaux | last post by:
What is the recommended pattern for implementing a synchronized (thread-safe) class that inherits from Collection<T>? For example, I want to implement a SyncRoot property . I do see where I can...
6
by: Doug | last post by:
Hi, I want to create a collection of objects that will be used as a property of another object. So for example if I had a Customer object, I would like to have an AddressCollection property...
0
by: xpding | last post by:
Hello, I have a class MyEmbededList contains a generic dictionary, the value field is actually the MyEmbededList type as well. There is another class need to access and manipulate a list of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.