multithreading - If I have 1 thread writing and 1 thread reading an int32, is it threadsafe? -


I am using C #, and I need to know that reading 1 thread and writing 1 thread is any Safe without vaporization or locks I only have to read / write 32 bit ints and booleans.

This is pretty much the definition of a thread-insecure code if you lock If you analyze the need, you always find this pattern. If you do not lock, the thread that reads will show random variations in the variable, not just outside its synchronization with its execution and completely at random times.

The guarantee that comes with the C # memory model is an int and bool update atom . An expensive word meaning that you will not be able to read any value by mistake, where some bytes of value have a new value and some have an old value. Which will produce completely random numbers. Each value update is atomic, long , double and the structure type (faucet & lt;> and decimal ) are not atoms.

Problems are not necessary to lock the issues where they are needed to debug, they depend on time and are arranged in the program execution pattern, where the time is not very different But which can suddenly change when the machine is captured by another task for example. Your program is running fine for a week, then once you receive an email message, it fails :) It is indispensable.


Comments

Popular posts from this blog

winforms - C# Form - Property Change -

javascript - amcharts makechart not working -

java - Algorithm negotiation fail SSH in Jenkins -