CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    The "volatile trick"

    A while back I read this column:
    http://www.drdobbs.com/cpp/184403766
    which proposes "stealing" the volatile keyword to aid in establishing thread safety. This usage has nothing to do with the actual meaning of volatile; it just exploits the fact that it's part of the C++ type system.

    Now, I came to the conclusion quickly that this was a bad idea. Neat, and I'd go with it in a second if there were a purpose-built keyword, but borrowing the volatile keyword for an unrelated use just seems to be asking for trouble; especially given the confusion which sometimes arises when Java programmers think that calling something volatile makes it thread safe (when it does nothing of the kind in C++).

    I would like the opinions of the experts on this matter, however, because it's interesting enough as a concept.

  2. #2
    Join Date
    Nov 2003
    Posts
    1,902

    Re: The "volatile trick"

    FWIW, an old (and long) thread, with Mr. Alexandrescu and other industry experts - http://groups.google.com/group/comp....878c22d7775997

    All other issues aside, my biggest gripe is the potential for confusion when putting the two together (volatile + multi-threading). Of course, MS giving multi-threaded semantics to volatile in MSVC v14/2005 didn't help the situation at all.

    gg

  3. #3
    Join Date
    Apr 2010
    Posts
    15

    Re: The "volatile trick"

    I don't consider myself any kind of expert in this area, but I would recommend a different keyword. For something that has been established for so long, additional usage will be confusing. And, if there are any (unforeseen) side effects, recompilation of codes with the original use of "volatile" could break applications that have worked up to that point.

    Why not something like "threadsafe" as the new keyword? It's only got 2 more letters than "volatile." :-)
    Clay Breshears
    Intel Innovative Software Education

    "There are no evil threads; just threads programmed for evil."

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: The "volatile trick"

    ^That'd be nice, but I think we have to stick with what the language provides now and in 1x.

    Quote Originally Posted by Codeplug View Post
    Of course, MS giving multi-threaded semantics to volatile in MSVC v14/2005 didn't help the situation at all.
    Ack, really? Don't spread it around. Maybe no one will notice.....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured