-
August 13th, 2010, 10:27 AM
#1
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.
-
August 13th, 2010, 11:13 AM
#2
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.
-
August 13th, 2010, 02:55 PM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|