c# - understanding of Volatile.Read/Write -
c# - understanding of Volatile.Read/Write -
i'm trying understand c# volatile class.
as read:
the volatile.write method forces value in location written @ point of call. in addition, before program-order loads , stores must occur before phone call volatile.write.
the volatile.read method forces value in location read @ point of call. in addition, later program-order loads , stores must occur after phone call volatile.read.
is means in case of:
internal sealed class threadssharingdata { private int32 m_flag = 0; private int32 m_value = 0; // method executed 1 thread public void thread1() { // note: 5 must written m_value before 1 written m_flag m_value = 5; volatile.write(ref m_flag, 1); } // method executed thread public void thread2() { // note: m_value must read after m_flag read if (volatile.read(ref m_flag) == 1) console.writeline(m_value); } } the cpu wait commands before volatile.write(ref m_flag, 1); before starting write m_flag?
and how helps threads synchronization?
the cpu wait commands before volatile.write(ref m_flag, 1); before starting write m_flag?
eeeh, kinda. improve way phrase is: it's guaranteed that, if other thread sees m_flag set 1, see m_value set 5.
and how helps threads synchronization?
i wouldn't helps synchronization - help achieving correctness.
if weren't using volatile reads/writes, possible compiler/runtime/cpu reorder 2 instructions in thread1 method, , programme able print either 0, 5 or nil @ all.
with volatile reads/writes, programme print either 5 or nil @ all, never 0. intended behaviour.
c# volatile thread-synchronization
Comments
Post a Comment