11

A few questions about accessing a local variable from multiple threads at once:

  1. I have multiple threads writing and reading the value of a variable, should i synchronize access to it or not?

  2. The variable is being updated every few seconds from Thread1 and its being read and updated to the Database every few seconds from Thread2.

  3. Which problems can occur if i don't hold any logic and don't have any concurrency issues?

  4. Should i use volatile for this?

EDIT:

I would like to emphasize that i don't have any concurrency issues. Here's my specific scenarion:

a. My variable's name is pingLatency and it measures ping latency

b. Thread1 is sending a ping to 8.8.8.8 each 10 seconds and writes the latency to pingLatency

c. Thread2 updates a correcposing field with the value of pingLatency each 10 seconds.

d. Thread2 updates the same database row each time.

Now, i'm using this database field to monitor network connectivity. My question is - Can there be a situation where the variable is not updated or it would throw an exception due to thread safety issues? I want to avoid using lock because it just seems like an overkill.

What do you think?

5
  • 1
    Answers: 1. It depends; 2. It depends; 3. Lots; 4: Probably. Have you tried searching Stack Overflow for existing questions discussing this? There are many many questions related to thread safety here. Commented Jul 31, 2013 at 16:30
  • 1
    What, exactly, do you mean by "local variable"? Post an example. Commented Jul 31, 2013 at 16:30
  • I'm assuming that when you say variable you actually mean field, because a variable is always local, and then you wouldn't be able to access that from multiple threads. Commented Jul 31, 2013 at 16:31
  • I checked @Mgets as the correct answer because of the link in the first line. Commented Jul 31, 2013 at 17:01
  • 2
    @lasse you can access a local from multiple threads no problem. And no, a variable is not always local. A variable is any storage location. Commented Jul 31, 2013 at 17:20

3 Answers 3

8
  1. Yes you should synchronize access to it, if it is a primitive type there are methods to do this for you without locks
  2. no comment
  3. not sure by what you mean by this... most likely you'll end up inserting the wrong value into the DB
  4. Don't use volatile, per Eric Lippert, it's overly complicated and the semantics are very weird.

Be careful of breaking the memory model, C# by and large follows most other languages in using sequential consistency for data-race-free programs (SC-DRF). Volatile breaks this, so just use locks to prevent a data race.

As for lock it's not as heavy as one might imagine, in most cases the lock won't be contended in the scenario you imagine. So acquiring the lock should be painless in most cases.

Sign up to request clarification or add additional context in comments.

7 Comments

Eric didn't say that you shouldn't use volatile, he said that it probably doesn't do what you think it does. volatile is completely appropriate if you know what it means and does.
@LasseV.Karlsen given that the documentation says something different than Eric, I'm going to go with avoid volatile as it means that even MS isn't sure what volatile means. Also Eric is very clear that volatile breaks SC-DRF.
Volatile does imply atomic in that only atomic fields may be volatile.
@EricLippert I could be reading your old blog wrong but it appeared that volatile had issues with SC-DRF, particularly with both the sequential consistency, and data race bits...
Volatile does not make non atomic updates into atomic updates. For example, two concurrent x++ on a volatile field can still lose an update. But each read and write will be atomic. They cannot be torn.
|
1

If you want .NET managed parallelism use the built in good stuff. Task Parallelism. This will manage the threads for you and you can use the thread safe variables that are built in just as an array/list would be equal to ConcurrentBag, etc.

Comments

0

If access to your variable is atomic and there are no logical problems you are OK. according to this you can know if you are using an atomic variable.

2 Comments

That is assuming he knows about such things as memory fences, the use of volatile fields, etc.
Interlocked Variable Access - this is an updated link, as msdn links are out-dated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.