9

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?

Uri Abramson
  • 6,005
  • 6
  • 40
  • 62
  • 1
    Answers: 1. It depends; 2. It depends; 3. Lots; 4: Probably. Have you tried searching [so] for existing questions discussing this? There are many many questions related to thread safety here. – Lasse V. Karlsen Jul 31 '13 at 16:30
  • 1
    What, exactly, do you mean by "local variable"? Post an example. – John Saunders Jul 31 '13 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. – Lasse V. Karlsen Jul 31 '13 at 16:31
  • I checked @Mgets as the correct answer because of the link in the first line. – Uri Abramson Jul 31 '13 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. – Eric Lippert Jul 31 '13 at 17:20

3 Answers3

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.

Brian
  • 25,523
  • 18
  • 82
  • 173
Mgetz
  • 5,108
  • 2
  • 33
  • 51
  • 2
    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. – Lasse V. Karlsen Jul 31 '13 at 16:36
  • @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. – Mgetz Jul 31 '13 at 16:37
  • 2
    Volatile does imply atomic in that only atomic fields may be volatile. – Eric Lippert Jul 31 '13 at 17:22
  • @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... – Mgetz Jul 31 '13 at 17:26
  • 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. – Eric Lippert Jul 31 '13 at 17:31
  • @EricLippert - the posted URL to your blog is offline, could you update the URL? is it this: https://ericlippert.com/2011/05/26/atomicity-volatility-and-immutability-are-different-part-one/ – Rand Random Feb 13 '20 at 18:31
  • @RandRandom: That's correct; thanks for the note. I am gradually transitioning all that content over to ericlippert.com but it will take a while! – Eric Lippert Feb 13 '20 at 18:37
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.

Cubicle.Jockey
  • 3,288
  • 1
  • 19
  • 31
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.

asafrob
  • 1,838
  • 13
  • 16
  • 1
    That is assuming he knows about such things as memory fences, the use of volatile fields, etc. – Lasse V. Karlsen Jul 31 '13 at 16:35
  • [Interlocked Variable Access](https://learn.microsoft.com/en-us/windows/win32/sync/interlocked-variable-access) - this is an updated link, as msdn links are out-dated. – Gavin Williams Aug 02 '23 at 06:26