MSDN volatile sample

This is a discussion on MSDN volatile sample within the C++ Programming forums, part of the General Programming Boards category; Hi Codeplug, But the #42 post does not mention how to use volatile together with multi-threading programming. :-) This is ...

  1. #31
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi Codeplug,


    But the #42 post does not mention how to use volatile together with multi-threading programming. :-)

    This is the key points we discuss and do not agree with each other at the beginning. I am not sure whether you could give your final points of how to use volatile in multi-threading environment?

    Quote Originally Posted by Codeplug View Post
    In a previous thread I posted, Alexander Terekhov summed up legal usage of volatile - down in post 42.
    http://groups.google.com/group/comp....1a34f888b85281

    The last time I used volatile (correctly) was in an embedded system using a MIPS CPU. This CPU used memory mapped registers for all the on-CPU functionality. Even the interrupt mask register could be accessed by via a *special* memory address, even though it could be referenced explicitly in assembly. Basically the way it worked is the CPU would "intercept" any access to the address bus if the address was a *special* address - it would then re-route to access to the mapped register. In C, the register could then be directly read and written to using a volatile pointer. The reason the pointer is volatile is so that the compiler can't re-order accesses and so that each access in C code translates to an access to the address bus.

    gg

    regards,
    George

  2. #32
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Great reply, Codeplug!


    I have taken a long time to read it. And really learned a lot. Since you mentioned, there is no use to use volatile in multi-threading environment, I am not sure whether you have read this article or not from my other reply.

    http://www.ddj.com/cpp/184403766

    The points from the article are,

    (you can refer to section "volatile, Critical Sections, and Race Conditions" of this article)

    1. No need to use volatile for thread shared data in synchronized section (e.g. mutex and critical section);
    2. Need to use volatile for thread shared data in non-synchronization section.

    I am not sure whether you have any comments. :-)

    Quote Originally Posted by Codeplug View Post
    Conclusion? In my opinion, volatile has no use in multi-threaded programming. MSVC is the only compiler I know of that actually defines volatile semantics in a multi-threaded environment - and even then MS themselves admit that "volatile is of limited use".

    gg

    regards,
    George

  3. #33
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Come on. Are we going to continue posting 2/3/4/5 times in a row?
    Press the quote button, copy the contents in the reply box, cancel, press edit, paste, write a new reply.
    You do realize that double posting is usually frowned upon on forums, unless under certain circumstances. Thrice or more even more so frowned upon and disliked.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #34
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi brewbuck,


    I have a useful Dr. Dobb article of this topic, and I post the related points to #32 post of this thread. It is appreciated if you could review and comment whether you agree with the points or not. :-)

    Quote Originally Posted by brewbuck View Post
    Codeplug, you made an interesting post. But this does not change the fact that the in-memory value of a volatile variable is guaranteed to be equal to the semantic value of that variable at each sequence point, I believe that IS guaranteed by the standard. You are correct that volatile alone is not always enough to guarantee safety in multithread contexts, but it is definitely required.

    regards,
    George

  5. #35
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi brewbuck,


    What do you mean "dummy function"? Could you show some pseudo code please?

    Quote Originally Posted by brewbuck View Post
    The only thing I took away from that discussion was that you don't need volatile because you can always make a call to a "dummy function" which forces the compiler to synchronize the memory location. While true, it seems like a dorky hack.

    regards,
    George

  6. #36
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi CornedBee,


    It is appreciated if you could comment what exactly do you mean "implementation-defined area" in your below reply. I am still unclear after reading your reply for a couple of times. :-)

    Quote Originally Posted by CornedBee View Post
    You're always at the mercy of the implementation. C++03 doesn't have the concept of parallel execution. The memory model does not support it. The execution model does not support it. The moment you call any thread creation function, you're in implementation-defined area.

    regards,
    George

  7. #37
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi iMalc,


    Quote Originally Posted by iMalc View Post
    I might not go overboard and use it everywhere I think there might otherwise be a problem, but you can be damn sure that if there is an intermittent problem in my multi-threaded code that it'll be one of the things I'll try!
    As you mentioned above, have you got any result after trying? :-)


    have a good weekend,
    George

  8. #38
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Another 4 posts in a row. Use multi-quote or edit!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #39
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Wow, your reaction to my PM lasted a whopping single post.

    What I mean is that C++ isn't multi-threaded. The idea isn't there in the C++ execution model. The moment you create a second thread, the C++ standard has nothing more to say about the behaviour of your program. It's all up to your specific environment.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MSDN template sample
    By George2 in forum C++ Programming
    Replies: 12
    Last Post: 03-11-2008, 04:47 AM
  2. MSDN const_cast sample
    By George2 in forum C++ Programming
    Replies: 7
    Last Post: 12-17-2007, 08:32 AM
  3. MSDN OLE DB Sample Provider entry point
    By George2 in forum C++ Programming
    Replies: 0
    Last Post: 07-21-2007, 08:30 AM
  4. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  5. MSDN Searching Tips
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-19-2004, 05:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21