multithreading - If I have 1 thread writing and 1 thread reading an int32, is it threadsafe? -
multithreading - If I have 1 thread writing and 1 thread reading an int32, is it threadsafe? -
i using c#, , need know if having 1 thread reading, , 1 thread writing safe without using volatiles or locks. reading/writing 32 bit ints , booleans.
this pretty much definition of thread-unsafe code. if analyze need lock in threaded code pattern. if don't lock thread reads observe random changes variable, out of sync execution , @ random times.
the guarantee c# memory model int , bool update atomic. expensive word means not accidentally read value of bytes in value have new value , have old value. produce random number. not every value update atomic, long, double , construction types (including nullable<> , decimal) not atomic.
not locking necessary produces extremely hard debug problems. depend on timing , programs tend settle execution patterns timing doesn't vary much. can change, when machine occupied task example. programme running fine week, fail 1 time when email message :) undebuggable.
multithreading thread-safety int
Comments
Post a Comment