关闭

Interlocked Variable Access

标签: 原子锁
490人阅读 评论(0) 收藏 举报
分类:

Applications must synchronize access to variables that are shared by multiple threads.
Applications must also ensure that operations on these variables are performed atomically (performed in their entirety or not at all.)
应用程序必须同步访问被多个线程共享的变量.应用程序必须确保在共享变量的操作是原子性.要么全部执行,要么完全不执行.


Simple reads and writes to properly-aligned 32-bit variables are atomic operations
In other words, you will not end up with only one portion of the variable updated;
all bits are updated in an atomic fashion.
However, access is not guaranteed to be synchronized.
If two threads are reading and writing from the same variable, you cannot determine if one thread will perform its read operation before the other performs its write operation.
对于内存对齐的32bit变量操作的读和写是原子性的.换言之,对于32bit变量的操作不会只更新一部分.所有位都说原子性更新.
但是,这并不能确保同步的访问.如果两个线程同时对变量读写操作,那么就不能决定一个读操作是在写操作完成后进行的.

Simple reads and writes to properly aligned 64-bit variables are atomic on 64-bit Windows.
Reads and writes to 64-bit values are not guaranteed to be atomic on 32-bit Windows.
Reads and writes to variables of other sizes are not guaranteed to be atomic on any platform.
在64位window操作系统上面读写64位变量是原子性操作.但是在32位windows操作系统上面读写64位变量,则不确保操作是原子性的.
在任何平台上面,读写其他长度的变量的操作都不一定是原子性的.
 
The Interlocked API
The interlocked functions provide a simple mechanism for synchronizing access to a variable that is shared by multiple threads.
They also perform operations on variables in an atomic manner.
The threads of different processes can use these functions if the variable is in shared memory.
原子锁函是多个线程同步访问共享变量的一种简单机制.
The InterlockedIncrement and InterlockedDecrement functions combine the steps involved in incrementing or decrementing a variable into an atomic operation.

This feature is useful in a multitasking operating system, in which the system can interrupt one thread execution to grant a slice of processor time to another thread.
Without such synchronization, two threads could read the same value, increment it by 1, and store the new value for a total increase of 1 instead of 2.
The interlocked variable-access functions protect against this kind of error.

原子锁在多任务操作系统中非常有价值.系统可以一个线程在执行期间不会被另外一个线程打断.
如果不同步情况下,如果两个线程同时读取一个变量,并把该变量加1.结果将是有可能是增加1 而不是增加2. 原子锁将确保变量的访问不会发生这种错误.

Most of the interlocked functions provide full memory barriers on all Windows platforms.
There are also functions that combine the basic interlocked variable access operations with the acquire and release memory ordering semantics supported by certain processors.
Each of these functions contains the word "Acquire" or "Release" in their names;
for example, InterlockedDecrementAcquire and InterlockedDecrementRelease.
Acquire memory semantics specify that the memory operation being performed by the current thread will be visible before any other memory operations are attempted.
Release memory semantics specify that the memory operation being performed by the current thread will be visible after all other memory operations have been completed.
These semantics allow you to force memory operations to be performed in a specific order. Use acquire semantics when entering a protected region and release semantics when leaving it.

Acquire Memory Semantics 当内存正在被当前线程操作时,其他内存操作在尝试时,内存是可见性的.
Release memory semantics 当内存正在被当前线程操作时,只有当前线程内存操作完成时,内存才是可见的.

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:130284次
    • 积分:2614
    • 等级:
    • 排名:第14918名
    • 原创:120篇
    • 转载:4篇
    • 译文:21篇
    • 评论:11条
    最新评论