public static Queue<string> - is thread safe ?

  • Thread starter Thread starter GrahamS
  • Start date Start date
G

GrahamS

Hi,

Using CF3.5

Can someone please confirm that a queue defined in this manner is indeed
threadsafe in Compact Framework ??.

CF does not support 'Synchronized' - and I need to ensure that my queues are
indeed thread-safe.

If not - is there any advice on how to protect 'Typed' queues ?? - as they
also do not support 'SyncRoot' ??.

This 'could' be related to other issues I am posting re random app crashes
:-O.

Many Thanks

Regards

Graham
 
Nothing is thread-safe without some kind of synchronization code. public
static Queue<string> is not thread-safe. The CF (from 2.0) does support
SyncRoot. But with the generic Queue<T> class you have to cast to
ICollection then access SyncRoot from there ie something like the following
is required:

Queue<string> myQueue = new Queue<string>();
ICollection syncedCollection = myQueue;

lock(syncedCollection.SyncRoot)
{
//Do stuff.
}
 
Nothing is thread-safe without some kind of synchronization code. public
static Queue<string> is not thread-safe. The CF (from 2.0) does support
SyncRoot. But with the generic Queue<T> class you have to cast to
ICollection then access SyncRoot from there ie something like the following
is required:

Queue<string> myQueue = new Queue<string>();
ICollection syncedCollection = myQueue;

lock(syncedCollection.SyncRoot)
{
//Do stuff.
}
 
Simon,

Thanks for that update - it was the cast to ICollection that I missed - as
..Net on a PC has a 'Synchronized Queue' class - which CF doesn't.

I did see somewhere though that using statics was supposed to be thread-safe
in this environment :-O.

Thanks

Regards

Graham
 
Simon,

Thanks for that update - it was the cast to ICollection that I missed - as
..Net on a PC has a 'Synchronized Queue' class - which CF doesn't.

I did see somewhere though that using statics was supposed to be thread-safe
in this environment :-O.

Thanks

Regards

Graham
 
Quite the contrary. If anything is thread-safe it will be instance types
over static ones, except where you are using static classes that Microsoft
have written in the CF as some of them use thread syncronization behind the
scenes. The documentation usually confirms this because without
documentation or source code it is impossible to know.

You can have instance fields within static classes that works in some
scenarios but my general rule of thumb when using threading is that I always
use instance types and never static ones.

To be honest I only ever use static classes for factory implementations.
 
Quite the contrary. If anything is thread-safe it will be instance types
over static ones, except where you are using static classes that Microsoft
have written in the CF as some of them use thread syncronization behind the
scenes. The documentation usually confirms this because without
documentation or source code it is impossible to know.

You can have instance fields within static classes that works in some
scenarios but my general rule of thumb when using threading is that I always
use instance types and never static ones.

To be honest I only ever use static classes for factory implementations.
 
Back
Top