分享一个很好的 线程互斥的类

 

 

在使用网易云信duilib时,发现的这个互斥类,特此分享下

c++ 临界区对象

CRITICAL_SECTION

typedef struct _RTL_CRITICAL_SECTION {
    PRTL_CRITICAL_SECTION_DEBUG DebugInfo;

    //
    //  The following three fields control entering and exiting the critical
    //  section for the resource
    //

    LONG LockCount;
    LONG RecursionCount;
    HANDLE OwningThread;        // from the thread's ClientId->UniqueThread
    HANDLE LockSemaphore;
    ULONG_PTR SpinCount;        // force size on 64-bit systems when packed
} RTL_CRITICAL_SECTION, *PRTL_CRITICAL_SECTION;

typedef RTL_CRITICAL_SECTION CRITICAL_SECTION;

 

一般用法

// 定义临界区对象
CRITICAL_SECTION cs ; // 一般声明为一个类的成员

// 初始化临界区对象
InitializeCriticalSection(&cs); // 一般在构造函数中进行初始化

// 进入临界区
EnterCriticalSection(&cs);

// 离开临界区
LeaveCriticalSection(&cs);

// 临界区不用的时候,进行销毁释放占用资源
DeleteCriticalSection(&cs);

 

封装成类:

class BASE_EXPORT NLock
{
public:
#if defined(OS_WIN)
      typedef CRITICAL_SECTION OSLockType;
#elif defined(OS_POSIX)
      typedef pthread_mutex_t  OSLockType;
#endif

      NLock();
      ~NLock();

      // 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();

      // Return the native underlying lock.  Not supported for Windows builds.
#if !defined(OS_WIN)
      OSLockType* os_lock() { return &os_lock_; }
#endif

private:
      OSLockType os_lock_;
};
// Lock implemetation on posix like Mac OS X/Linux/FreeBSD

#include "lock.h"

#if defined(OS_POSIX)

#include <errno.h>

namespace nbase
{

NLock::NLock()
{
	// In release, go with the default lock attributes.
	pthread_mutex_init(&os_lock_, NULL);
}

NLock::~NLock()
{
	pthread_mutex_destroy(&os_lock_);
}

bool NLock::Try()
{
	int rv = pthread_mutex_trylock(&os_lock_);
	return rv == 0;
}

void NLock::Lock()
{
	pthread_mutex_lock(&os_lock_);
}

void NLock::Unlock()
{
	pthread_mutex_unlock(&os_lock_);
}

}  // namespace

#endif  // OS_WIN

 

// Lock implemetation on windows

#include "lock.h"

#if defined(OS_WIN)

namespace nbase
{

NLock::NLock()
{
	// The second parameter is the spin count, for short-held locks it avoid the
	// contending thread from going to sleep which helps performance greatly.
	::InitializeCriticalSectionAndSpinCount(&os_lock_, 2000);
}

NLock::~NLock()
{
	::DeleteCriticalSection(&os_lock_);
}

bool NLock::Try()
{
	if (::TryEnterCriticalSection(&os_lock_))
	{
		return true;
	}
	return false;
}

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

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

}  // namespace

#endif  // OS_WIN
class BASE_EXPORT NAutoLock
{
public:
	NAutoLock(NLock * lock)
	{
		assert(lock);
		lock_ = lock;
		lock_->Lock();
	}

	~NAutoLock()
	{
		if (lock_)
			lock_->Unlock();
	}

private:
	NLock *lock_;
};

class BASE_EXPORT NAutoUnlock
{
public:
	NAutoUnlock(NLock * lock)
	{
		assert(lock);
		lock_ = lock;
		lock_->Unlock();
	}

	~NAutoUnlock()
	{
		if (lock_)
			lock_->Lock();
	}

private:
	NLock *lock_;
};

 

 

使用,在线程互斥的地方

public:
	nbase::NLock  lock_;
	bool VideoFrameMng::GetVideoFrame(std::string account, int64_t& time, char* out_data, int& width, int& height, bool mirror, bool argb_or_yuv, bool bCut /*= false*/)
	{

		int nCtrlW = width-width%2;
		int nCtrlH = height-height%2;



		nbase::NAutoLock auto_lock(&lock_);


        ...

    }

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 成长之路 设计师:Amelia_0503 返回首页
实付 19.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值