In the C # parameter, the value of the variable is atomic if its size does not exceed native int (i.e. 4 bytes in a 32-bit runtime environment and 8 bytes in a 64-bit environment). In a 64-bit environment, which includes all types of links and most of the built-in value types ( byte , short , int , long , etc.).
Setting a larger value is not atomic and may cause breaks when only part of the memory is updated.
DateTime is a structure that includes only one ulong field containing all its data ( Ticks and DateTimeKind ) and ulong itself is atomic in a 64-bit environment.
Does this mean that DateTime is atomic? Or can the following code cause a break at some point?
static DateTime _value; static void Main() { for (int i = 0; i < 10; i++) { new Thread(_ => { var random = new Random(); while (true) { _value = new DateTime((long)random.Next() << 30 | (long)random.Next()); } }).Start(); } Console.ReadLine(); }
i3arnon Feb 15 '17 at 19:24 2017-02-15 19:24
source share