Grieve (grieve) wrote,
Grieve
grieve

Volatile does not thread safe make.

I keep running across code, some old and some new, that uses the volatile keyword in an attempt to make the code thread "safe". Using that is always suspect, and without a truly deep understanding of the underlying hardware I doubt you could ever prove it was safe. With todays modern dual core processors, you can be 100% certain that it is unsafe.

In a nutshell volatile means to fetch the value from memory, and ignore whatever may be in the cache. What does that mean when you have two caches? At any rate when I see these items in the code I remove them. What is doubly irritating about them is that I tend to see them in java. Where classes like AtomicInt and AtomicBoolean are part of the libraries.
Tags: programming, thread safety
Subscribe

  • Update FreeBSD

    I managed to borrow not one, but two CD-ROM drives, and updated my FreeBSD 4.11 box with 6.2. It went smoothly, and now I just need to restore…

  • FreeBSD update from 4.11 to 6.2

    My update is not going so well. I tried to build the new source tree, and it immediately breaks. I may have to buy a cheap CD-ROM drive just so I can…

  • One thing leads to another.

    At work I use Miranda as my IM client. Last week, at some indefinite point in time, it just stopped working. I talked with the local IT guys, and…

  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 2 comments