views:

322

answers:

4

Is a C# struct thread-safe?

For example if there is a:

struct Data
{
    int _number;
    public int Number { get { return _number; } set { _number = value; } }

    public Data(int number) { _number = number; }
}

in another type:

class DadData
{
    public Data TheData { get; set; }
}

is property named TheData, thread-safe?

A: 

No. Why would it be thread-safe? It's just data. It doesn't become thread-safe by magic.

John Saunders
+1  A: 

A struct is not any more thread-safe than an ordinary field or variable. If you have at least one thread modifying it, and at least one more thread touching it in any way at the same time, you may end up with unexpected/undefined behaviour.

Also, mutable structs are code smells. Is there some particular reason you need it to be a struct instead of a class? Do you need value-type semantics for this data?

Aaronaught
+4  A: 

No, structures in .NET are not intrinsically thread-safe.

However, the copy-by-value semantics that structures have great relevance to this converation.

If you are passing your structures around and assigning them in some way to variables or pass-by-value parameters (no ref or out keywords) then a copy is being used.

Of course, this means that any changes made to the copy are not reflected in the original structure, but it's something to be aware of when passing them around.

If you are accessing the structure directly in a manner that doesn't involve copy-by-value semantics (e.g. accessing a static field which is the type of the structure, and as Marc Gravel points out in his answer, there are many other ways) across multiple threads, then you have to take into account the thread-safety of the instance.

casperOne
And to make this worse, it also then depends a *great* deal whether your code talks to a *field*, a *variable* or a *property*. Luckily in this case the auto-implemented property (`TheData`) removes most of these. So I'm just mentioning it for completeness ;-p
Marc Gravell
Thank you casperOne! Thank you Marc! As casperOne has put it, I thought "the copy-by-value semantics that structures have great relevance to this conversation"; yet failed to see any instructions on refrences. This code is not my actual code, but a representation. I am writing some multi-threaded applications; and some common parallel-programming tools is C#; because some patterns are returning frequently ;) (For sure I will look for critiques here when I have came out with something worthy).
Kaveh Shahbazian
+3  A: 

Well - best practice is that structs should always (except in a few very specific scenarios, and even then at risk) be immutable. And immutable data is always thread safe. So if you followed best practice and made this:

struct Data
{
    readonly int _number;
    public int Number { get { return _number; } }

    public Data(int number) { _number = number; }
}

then yes; that is thread-safe. In all other cases the answer is "probably not".

Note also that atomicity rules apply, so even a single read or update to DadData.TheData cannot be assumed to be thread-safe, even with an immutable struct. You could (especially for oversized structs) have one thread reading the struct while another thread re-writes it; without synchronization bad things will happen (eventually).

Marc Gravell