Atomic operations

A

Armin Zingler

Hi,

is there a documentation out there that tells me which operations are atomic?

I currently have to know if increasing a System.Decimal value is an atomic operation. I guess it is
not. The value is increased in one thread and read from another thread. I think I have to use a
lock. Right?
 
P

Peter Duniho

Armin said:
Hi,

is there a documentation out there that tells me which operations are atomic?

Depends on what you mean. The C# specification, for example, is clear
about what operations within the language are atomic (certain
assignments and reads, specifically).

But of course it doesn't mention any of the System.Threading.Interlocked
class, for example, which also provides for certain atomic operations.
In that sense, no...there's not a single place that all atomic
operations are documented.
I currently have to know if increasing a System.Decimal value is an atomic operation.

An increment of any value is not guaranteed to be atomic unless you use
the Interlocked class, and the System.Decimal type is too wide to be
supported by the Interlocked class in any case.
I guess it is not. The value is increased in one thread and read from another thread. I think I have to use a
lock. Right?

You have to use some kind of synchronization, yes. And because it's so
wide, the System.Decimal type isn't even among those in C# that even has
atomic assignment.

There are a wide variety of possibilities, but a "lock" statement is
definitely the way to go, until you have some reason to believe you need
something cleverer and more efficient.

Pete
 
A

Armin Zingler

Peter said:
Depends on what you mean.

Atomic operations are operations on objects that are performed completely without
the possibility that another thread reads the object or a part of it in the meantime.
The C# specification, for example, is clear about what operations within the language
are atomic (certain assignments and reads, specifically).

I'm using VB (2008). I didn't know it's a language specific issue, therefore I posted here.
Didn't find something specific to VB.

Anyway, I've put reading/writing the value inside a Synclock-EndSynclock statement.
 
P

Peter Duniho

Armin said:
Atomic operations are operations on objects that are performed completely without
the possibility that another thread reads the object or a part of it in the meantime.

I know what "atomic operation" means. I was referring to your question
about "is there a documentation out there", and the answer depends on
what you mean by _that_. Atomic operations certainly are documented, so
the answer to your question is trivially "yes". But it might not really
be the question you meant to ask, thus my "it depends".
I'm using VB (2008). I didn't know it's a language specific issue, therefore I posted here.
Didn't find something specific to VB.

I'm less familiar with VB.NET, but I would hope it would have some
similar atomic guarantees for smaller data types (e.g. System.Int32,
System.Boolean, etc.). Still, I doubt any of those would apply to
System.Decimal.
Anyway, I've put reading/writing the value inside a Synclock-EndSynclock statement.

Should be fine. :)

Pete
 

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