加锁基类LockImpl

LockImpl是一个用于Chrome的加锁基类,它定义了尝试获取锁、锁定、解锁以及在调试模式下检查锁是否已被当前线程持有的方法。该类基于CRITICAL_SECTION实现,并使用InitializeCriticalSectionAndSpinCount进行初始化,DeleteCriticalSection进行销毁。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

chrome中的加锁基类,去掉不用的部分

 

#pragma once

#define DISALLOW_COPY_AND_ASSIGN(TypeName) /
 TypeName(const TypeName&);               /
 void operator=(const TypeName&)


class LockImpl
{
 typedef CRITICAL_SECTION OSLockType;

public:
 LockImpl(void);
 virtual ~LockImpl(void);

 // If the lock is not held, take it and return true.  If the lock is already
 // held by something else, immediately return false.
 bool Try();

 // Take the lock, blocking until it is available if necessary.
 void Lock();

 // Release the lock.  This must only be called by the lock's holder: after
 // a successful call to Try, or a call to Lock.
 void Unlock();

 // Debug-only method that will DCHECK() if the lock is not acquired by the
 // current thread.  In non-debug builds, no check is performed.
 // Because linux and mac condition variables modify the underlyning lock
 // through the os_lock() method, runtime assertions can not be done on those
 // builds.
#ifdef DEBUG
 void AssertAcquired() const;
#endif

private:
 OSLockType os_lock_;
 DISALLOW_COPY_AND_ASSIGN(LockImpl);
};

 

#include "stdafx.h"
#include "LockImpl.h"

LockImpl::LockImpl(void)
{
  ::InitializeCriticalSectionAndSpinCount(&os_lock_, 2000);
}

LockImpl::~LockImpl(void)
{
  ::DeleteCriticalSection(&os_lock_);
}

bool LockImpl::Try() {
 if (::TryEnterCriticalSection(&os_lock_) != FALSE) {
  return true;
 }
 return false;
}

void LockImpl::Lock() {
 ::EnterCriticalSection(&os_lock_);
}

void LockImpl::Unlock() {
 ::LeaveCriticalSection(&os_lock_);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值