I understand that different threads change the same variable, and there must be an Exception

public partial class MainWindow : Window { Int64 i = 0; Int64 j = 1; private void Test() { while (true) { j = -j; i += j; } } public MainWindow() { InitializeComponent(); for (int n = 0; n < 4; ++n) { Thread t = new Thread(() => Test()); t.Priority = ThreadPriority.Normal; t.Start(); } } } 
  • 2
    Arithmetic errors and / or incorrect behavior are possible. Why do you think there should be an Exception? - Kromster
  • one
    Nothing prevents threads from writing the same area of ​​memory. And in order to prevent, you need to make locks. And each thread must follow the rules of blocking, because nothing prevents it from spitting on these blockages. - Sergey
  • one
    And arithmetic, too, most likely will not. Unless of course you consider an incorrect result to be an arithmetic error due to an incorrect algorithm. - Sergey
  • one
    If -400 is received, what does this mean? Incorrectly folded 800 units or all the same correctly folded 400 "negative units"? This is not an arithmetic error. This is a mistake of logic, an algorithm that hopes for a clear sequence of arithmetic operations, but in fact they are executed in an unpredictable order. I now understand the division by 0 - an arithmetic error. Well, overflowing there may be possible - you can also write off arithmetic error. - Sergey
  • one
    it looks like you are confusing threads with processes. When trying to access the memory of another process, an exception should really occur, MemoryAccessViolation seems, but only if such access is not authorized by the OS. Threads, however, belong to the same process and share process memory. - rdorn

1 answer 1

Looks like you're confusing threads with processes. When trying to access the memory of another process, an exception should really occur, but only if such access is not authorized by the OS. Threads, however, belong to the same process and share the process memory, so there should be no exceptions in your code.