public static Queue<string> - is thread safe ?

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
 
S

Simon Hart [MVP]

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.
}
 
S

Simon Hart [MVP]

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.
}
 
G

GrahamS

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
 
G

GrahamS

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
 
S

Simon Hart [MVP]

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.
 
S

Simon Hart [MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top