Are memory barriers required when joining a stream?

If thread A spawns another thread B for the sole purpose of writing to variable V and then waits for it to complete, are memory barriers necessary to ensure that subsequent readings of V in thread A are fresh? I'm not sure if there are any implicit barriers in termination / join operations that make them redundant.

Here is an example:

public static T ExecuteWithCustomStackSize<T>
    (Func<T> func, int stackSize)
{
    T result = default(T);

    var thread = new Thread(
        () => 
                {
                    result = func();
                    Thread.MemoryBarrier(); // Required?
                }
        , stackSize);

    thread.Start();
    thread.Join();

    Thread.MemoryBarrier(); // Required?
    return result;
}

Are both (or more) barriers in the above fragment?

+5
source share
3 answers

No, synchronization mechanisms generate implicit memory barriers. All data modified by the stream will be visible after the stream is attached.

+3
source

, -

MemoryBarrier (, Intel Itanium).

# , Visual Basic SyncLock Monitor .

, .

0

. , , . "", .

, Join. , Join.

0

All Articles