tags:

views:

44

answers:

1

This MSDN article stated that:

processors are free to reorder this code

internal static volatile int s_x = 0;
internal static volatile int s_y = 0;
internal static volatile int s_ya = 0;

void ThreadA() {
    s_x = 1;
    s_ya = s_y;
}

I am worried if any code could be reordered, and I know how to identify reorderable code. Specially about this piece of my code:

Processing.Add(ItemNumber, null);
AsyncTask.Begin(InternalRequestCallback, Remove, ItemNumber);

It must occur in the specified order or else it fails. Should I put a Thread.MemoryBarrier(); between those statements?

+3  A: 

Short version: no.

You code simply perform two invokes; the runtime guarantees that those methods will execute (on a single thread, at least) in sequence.

The complexity of the article centred around the complexities of 2 threads accessing shared state (you need the ThreadB method for the example to make sense). The code you have pasted is, I'm pretty sure, not re-orderable - not least because this is successive instructions (on dependent data) on the same thread.

You'd need to get a lot deeper than I understand to go into the full details. But unless you're doing something complex with lock-free, threaded programming, this is not likely to bite you.

Marc Gravell