CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2010
    Posts
    1

    Question Behavior/Implementation of InterlockedXXX functions

    Hello to everyone in this forum.

    Sorry if this isn't the right place for this Post or the following has already been discused somewhere else.

    I have a question about the usage and implementation of the InterlockedXXX functions. On some documentations in the MSDN it's said, that these operations are atomic with repect to other Interlocked operations. This statement confuses me, because as far as I know these operations directly map to commands of the CPU, and these commands are atomic with respect to all operations. (because of the lock prefix).
    So what does this statement really mean, is ist that for the possibility, that the Windows API will maby be ported in the Future to some CPU, on which it is really just possible to realize the InterlockedXXX functions in a way, that the "with respect to other interlocked operations" statment applies, or is there something else, I do not understand?

    Thank you, if you can give me answers to my question.
    Bye.

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Behavior/Implementation of InterlockedXXX functions

    Changing the same variable in two different threads using InterlockedXXX function is thread-safe. But if you change the variable in one thread using InterlockedXXX function, and in another thread using plain C code, this is not thread-safe. I understand "with respect to other interlocked operations" by such way.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Behavior/Implementation of InterlockedXXX functions

    That sounds reasonable, Alex.

    In other words, if you want to make a variable threadsafe using the InterLocked functions, then you need to use them everywhere the variable gets changed (and not allow the variable to be changed directly).

    A simple way to do this is put the variable inside a class as a private variable and expose public get/set methods that internally use the InterLocked functions.

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