volatile structures

T

titan nyquist

Can you make volatile structures in C#?

I have a static class, to have "global" variables. This allows the
whole program to see them. I make them "volatile" to avoid multi-
threading accessing issues.

That works.

THE PROBLEM: In that static class, I want to combine some variables
inside a structure, and then make a variable of that structure type,
and make it volatile.

But it doesn't work:


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

namespace VolatileStructuresTest
{
class Program
{
static void Main(string[] args)
{
TestClass.a = 1;
Console.WriteLine("a = " + TestClass.a);

TestClass.testStruct.b = 2;
Console.WriteLine("b = " + TestClass.testStruct.b);
}
}
}

static class TestClass
{
static volatile public int a;

public struct TestStruct
{
public int b;
};

static /*volatile*/ public TestStruct testStruct = new TestStruct();
}
------------------------------------------------------



Uncomment out /*volatile*/, in the last line of code, to get this
error:
'TestClass.testStruct': a volatile field cannot be of the type
'TestClass.TestStruct'

Why is this?

Titan
 
K

Kevin Spencer

No, you can not. Here are the types you can make volatile:

1. Reference Types.
2. Type parameters that are known to be reference types.
3. Types byte, sbyte, short, ushort, int, uint, char, float, bool
4. enums that have a base type from the list in number 3.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
W

Willy Denoyette [MVP]

Kevin Spencer said:
No, you can not. Here are the types you can make volatile:

1. Reference Types.
2. Type parameters that are known to be reference types.
3. Types byte, sbyte, short, ushort, int, uint, char, float, bool
4. enums that have a base type from the list in number 3.

Note the inconsistency, a reference type can be volatile, while a long cannot, a reference
type on a 64 bit OS is effectively a long.

Willy.
 
J

Jon Skeet [C# MVP]

titan nyquist said:
Can you make volatile structures in C#?

No. From the C# spec:

<quote>
The type of a volatile field must be one of the following:
* A reference-type.
* The type byte, sbyte, short, ushort, int, uint, char, float, or
bool.
* An enum-type having an enum base type of byte, sbyte, short,
ushort, int, or uint.
</quote>

Volatility usually relies on some processor abilities which won't be
available for arbitrarily-sized data structures.

Personally, I very rarely use volatile. It only solves a certain type
of threading issue, and it can be quite difficult to tell when it's
enough and when you should use locks - so I almost always use locks.
 
T

titan nyquist

Thanks for the replies.

If I have a bunch of volatile variables in a static class, and I want
to group them together, I cannot make a struct out of them and keep
the volatile definition... but, the struct is much cleaner in terms of
data structuring. So, should I generally go ahead and make the
struct, ignore the volatile, and try to solve any potential multi-
thread isses with lock?
 
J

Jon Skeet [C# MVP]

titan nyquist said:
Thanks for the replies.

If I have a bunch of volatile variables in a static class, and I want
to group them together, I cannot make a struct out of them and keep
the volatile definition... but, the struct is much cleaner in terms of
data structuring. So, should I generally go ahead and make the
struct, ignore the volatile, and try to solve any potential multi-
thread isses with lock?

Well, I would strongly consider making it a class rather than a struct,
unless you *really* want value-type semantics. Then yes, look at the
multi-threading issues and see whether locking is the best way forward.
 
T

titan nyquist

Ok, thanks. I'll look into making it a class. If it works and it's
clean and clear, then I don't need a struct.

Titan
 

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