views:

269

answers:

7

I have the feeling that I should not care about thread safe accessing / writing to an

public static int MyVar = 12;

in ASP .NET.

I read/write to this variable from various user threads. Let's suppose this variable will store the numbers of clicks on a certain button/link.

My theory is that no thread can read/write to this variable at the same time. It's just a simple variable of 4 bytes.

I do care about thread safe, but only for refference objects and List instances or other types that take more cycles to read/update.

I am wrong with my presumption ?

EDIT

I understand this depend of my scenario, but wasn't that the point of the question. The question is: it is right that can be written thread safe code with an (static int) variable without using lock keyword ?

It is my problem to write correct code. The answer seems to be: Yes, if you write correct and simple code, and not to much complicated, you can create thread safe functions without the need of lock keyword.

+1  A: 

A simple read or write on a field of 32 bits or less is always atomic. But you should provide your read/write code to make sure that it is thread safe.

Darin Dimitrov
So, my presumption is correct ? I saw questions on SO about how to create thread safe INT variables
pixel3cs
Without seeing your code that manipulates the variable I cannot guarantee you it is thread safe.
Darin Dimitrov
But ! it is right that can be written thread safe code with an (static int) variable without using lock keyword ?
pixel3cs
Of course, if you only use this variable for reading.
Darin Dimitrov
+1  A: 

Check out this post: http://msdn.microsoft.com/en-us/magazine/cc163929.aspx

It explains why you need to synchronize access to the integers in this scenario

Scobal
+2  A: 

The concept of thread 'safety' is too vague to be meaningful unfortunately. If you're asking whether you can read and write to it from multiple threads without the program crashing during the operation, the answer is almost certainly yes. If you're also asking if the variable is guaranteed to either be the old value or the new value without ever storing any broken intermediate values, the answer for this data type is again almost certainly yes.

But if your question is "will my program work correctly if I access this from multiple threads", then the answer depends entirely on what your program is doing. For example, if you run the following pseudo code in 2 threads repeatedly in most programming languages, eventually you'll hit the assertion.

if MyVar >= 1:
   MyVar = MyVar - 1
   assert MyVar >= 0
Kylotan
To the original poster: Your program won't crash on concurrent read/writes to an Int32, however if you use operations like x++ they may not operate as you expect.
Aviad P.
yes, I was referring at points 1 and 2, where your answer is Yes. It is my problem as a programmer to write write correct code, as you showed me on point 3
pixel3cs
The problem is, the code above is correct - if it's single threaded. And the only thing that makes it incorrect for multithreaded code is exactly what you're asking about: reading and writing from separate threads. That's why the term 'thread safe' is a bad one to use, because you need to specify what operation you're performing, as well as your definition of safety for it.
Kylotan
+2  A: 

If one thread simply sets the value and another thread reads the value, then a lock is not necessary; the read and write are atomic. But if multiple threads might be updating it and are also reading it to do the update (e.g., increment), then you definitely do need some kind of synchronization. If only one thread is ever going to update it even for an increment, then I would argue that no synchronization is necessary.

Mark Wilkins
one thread is for setting, and 10 threads are for reading. I thing there is no problem with that. I can write thread safe code using an int without the need of lock keyword in this case !
pixel3cs
Thanks. 1 or many thread to read vs 1 thread to update - no lock needed, 1 or many thread to read vs many threads to update - lock is needed
pixel3cs
+1  A: 

Try Interlocked.Increment() or Interlocked.Add() and you'll be right. Your code complexity will be the same but you truly won't have to worry. If you're not worried about losing a few clicks in your counter, you can continue as you are.

No Refunds No Returns
+1  A: 

Primitives like int are thread-safe in the sense that reads/writes are atomic. But as with most any type, it's left to you to do proper checking with more complex operations. For example, if (x > 0) x--; would be problematic in a multi-threaded scenario because x might change in between the if condition check and decrement.

Joel Coehoorn
I agree with you. But if no checking is made, and I only read/write and present the variable on screen, I should not care about syncronization.
pixel3cs
+1  A: 

Reading or writing integers is atomic. However, reading and then writing is not atomic. So, if you have one thread that writes and many that read, you may be able to get away without locks.

However, even though the operations are atomic, there are still potential multi-threading issues. In order for one thread to be guaranteed that another thread can see values it writes, you need a memory barrier. Otherwise, the compiler can optimize the code so that the variable stays in a register (or even optimize the operation away completely), so changes would be invisible from one thread to another.

You can establish a memory barrier explicitly (volatile or Thread.MemoryBarrier), or with the Interlocked class -- or with the lock statement (Monitor).

RickNZ