0

I am using double variables at the moment to use them as counters.

However my application is multi-threaded so if the operation be atomic that is a lot better for me

I can use however numbers can pass int limits and i do not need precision after . since they will be used as counter

Q: By precision is it always mentioned after . ? I mean operations lesser than 1 ?

Q: So my question is what could be the reason i want to use double instead of float ?

My cpu is latest gen core i 7 2600k 64 bit, application is also 64 bit

Can we say that double on 64 bit application is thread safe ?

.net 4.5 c# wpf application

Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342
  • 1
    Your post seems to be about two things: (A) the numerical characteristics of floating-point, and (B) thread-safety. These are pretty unrelated topics, I'd suggest splitting them into two dedicated SO questions. – Oliver Charlesworth Aug 21 '14 at 11:21
  • We can say *double on 64 bit application* is **atomic** not **thread safe**. Also it depends on what do you meant by [thread safe](http://blogs.msdn.com/b/ericlippert/archive/2009/10/19/what-is-this-thing-you-call-thread-safe.aspx) – Sriram Sakthivel Aug 21 '14 at 11:23
  • 6
    If you don't care about sub-integer values, why are you using either float *or* double? Why not int or long? Note that if you try to add 1 to a large float or double, you'll just get back the same value... in other words, for large values, the difference between two "nearest" values is more than 1. – Jon Skeet Aug 21 '14 at 11:23
  • @SriramSakthivel: Can we? (http://stackoverflow.com/questions/2736541/is-variable-assignment-and-reading-atomic-operation) – Oliver Charlesworth Aug 21 '14 at 11:28
  • @JonSkeet very good point i should use longs never used before :) i also tested and non is thread safe if you use variable++ . So my another question should i use a lock object or lock these variables itself ? – Furkan Gözükara Aug 21 '14 at 11:30
  • Maybe [Interlocked.Increment](http://msdn.microsoft.com/library/zs86dyzy.aspx) is what you're looking for. – Corak Aug 21 '14 at 11:32
  • 1
    @MonsterMMORPG: Look at the `Interlocked` class... note that `++` is *never* thread-safe, even for atomic types - because it's read/modify/write, and other threads can get in in-between. – Jon Skeet Aug 21 '14 at 11:32
  • @Corak seems like what i am looking for exactly ty very much – Furkan Gözükara Aug 21 '14 at 11:34
  • @OliCharlesworth Yes, we can. That question assumes it is x86. I said 64 bit application. And question is also for the same here. Refer [this](http://stackoverflow.com/a/11745471/2530848) – Sriram Sakthivel Aug 21 '14 at 11:35
  • If use Interlocked.Increment would it be still thread safe to read long values ? @JonSkeet – Furkan Gözükara Aug 21 '14 at 11:43
  • 2
    @MonsterMMORPG: You'd use `Interlocked.Read`. – Jon Skeet Aug 21 '14 at 11:44
  • @JonSkeet ty very much for answer. Can you also take a look at this question : http://stackoverflow.com/questions/25417084/system-threading-timer-working-logic-and-if-an-unexpected-error-happens-how-to-h – Furkan Gözükara Aug 21 '14 at 12:07

0 Answers0