Windows用户模式下的线程同步

Interlocked系列函数
  • 原子访问:线程在访问某个资源的时候能保证没有其他线程会在同一时刻访问同一资源
函数名 功能
InterlockedExchangeAdd
InterlockedExchangeAdd64
增/减
InterlockedExchange
InterlockedExchange64
InterlockedExchangePointer
交换
InterlockedCompareExchange16
InterlockedCompareExchange
InterlockedCompareExchange64
InterlockedCompareExchangePointer
比较赋值
InterlockedAnd16
InterlockedAnd
InterlockedAnd64
InterlockedOr16
InterlockedOr
InterlockedOr64
InterlockedXor16
InterlockedXor
InterlockedXor64
异或

注意点:

  • InterlockedXxx is atomic only with respect to other InterlockedXxx calls.
  • The parameters for this function must be aligned on a 32-bit boundary; otherwise, the function will behave unpredictably on multiprocessor x86 systems and any non-x86 systems. See _aligned_malloc.(上述部分函数在MSDN解释里有这句话)
  • 使用_aligned_malloc分配一块对齐过的内存。对应释放函数:_aligned_free


关键段
函数名 功能
InitializeCriticalSection 初始化
EnterCriticalSection 等待直至获取到关键段的所有权
LeaveCriticalSection 释放关键段的所有权
DeleteCriticalSection 资源释放
TryEnterCriticalSection 尝试获取关键段的所有权,此函数不会等待
InitializeCriticalSectionAndSpinCount 初始化具有螺旋锁的关键段
备注:线程被切换到等待前会先进行旋转,从而可能会提高性能
SetCriticalSectionSpinCount 设置关键段的螺旋锁的旋转次数
备注:用来保护进程堆的关键段所使用的旋转次数大约是4000
EnterCriticalSection注意事项
  • 此函数超时会引发异常,超时时间在注册表中可以设置(默认30天左右)
  • 线程可以在EnterCriticalSection成功后继续EnterCriticalSectionTryEnterCriticalSection,每一次成功获取访问权都需要对应使用LeaveCriticalSection
  • 当线程调用EnterCriticalSection时,关键段的访问权已被占用,那么关键段会在内部使用一个事件内核对象,只有在第一次要用到事件内核对象时,系统才会去创建他(当进程能使用的内存过低时,这个创建可能会失败,从而会抛出异常),此时系统会切换到内核模式,此过程开销很大。为此,使用附带螺旋锁的关键段的性能可能会比不使用螺旋锁的关键段的性能高
InitializeCriticalSection注意事项
  • 在进程能使用的内存过低时,此函数可能会失败并会抛出异常
InitializeCriticalSectionAndSpinCount注意事项
  • 此函数的旋转次数的最高位设为1,则会在初始化时创建一个与关键段相关联的事件内核对象,从而可能防止EnterCriticalSection调用时因为事件内核对象分配问题而抛出异常


Slim 读/写锁
函数名 功能
InitializeSRWLock Initialize a slim reader/writer (SRW) lock.
AcquireSRWLockExclusive Acquires a slim reader/writer (SRW) lock in exclusive mode.
AcquireSRWLockShared Acquires a slim reader/writer (SRW) lock in shared mode.
ReleaseSRWLockExclusive Releases a slim reader/writer (SRW) lock that was acquired in exclusive mode.
ReleaseSRWLockShared Releases a slim reader/writer (SRW) lock that was acquired in shared mode.
TryAcquireSRWLockExclusive Attempts to acquire a slim reader/writer (SRW) lock in exclusive mode. If the call is successful, the calling thread takes ownership of the lock.
Win7以上才支持
TryAcquireSRWLockShared Attempts to acquire a slim reader/writer (SRW) lock in shared mode. If the call is successful, the calling thread takes ownership of the lock.
Win7以上才支持
注意点
  • SRW locks do not need to be explicitly destroyed.
  • An SRW lock is the size of a pointer. The advantage is that it is fast to update the lock state. The disadvantage is that very little state information can be stored, so SRW locks cannot be acquired recursively(递归地). In addition, a thread that owns an SRW lock in shared mode cannot upgrade(提升) its ownership of the lock to exclusive mode.
  • SRWLock系列函数不支持Windows Xp,支持的最低版本是Windows Vista,关键段支持Windows Xp
  • 线程使用Slim读/写锁相较于关键段更具有可伸缩性
  • 参考资料 https://docs.microsoft.com/en-us/windows/desktop/Sync/slim-reader-writer--srw--locks

你可能感兴趣的:(Windows用户模式下的线程同步)