471,119 Members | 1,449 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,119 software developers and data experts.

volatile structures

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

Apr 6 '07 #1
6 3348
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

"titan nyquist" <ti***********@gmail.comwrote in message
news:11**********************@b75g2000hsg.googlegr oups.com...
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

Apr 6 '07 #2
"Kevin Spencer" <un**********@nothinks.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
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.

Apr 6 '07 #3
titan nyquist <ti***********@gmail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 6 '07 #4
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?

Apr 8 '07 #5
titan nyquist <ti***********@gmail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 8 '07 #6
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

Apr 8 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by newsock | last post: by
8 posts views Thread by Tim Rentsch | last post: by
14 posts views Thread by Ian Pilcher | last post: by
14 posts views Thread by google-newsgroups | last post: by
17 posts views Thread by dingoatemydonut | last post: by
6 posts views Thread by red floyd | last post: by
94 posts views Thread by Samuel R. Neff | last post: by
3 posts views Thread by Rakesh Kumar | last post: by

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.