tars源码漫谈第24篇------tc_lock.h(基本锁)

2024-02-06 11:08

本文主要是介绍tars源码漫谈第24篇------tc_lock.h(基本锁),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

      这个文件很简单, 来看下:

/*** Tencent is pleased to support the open source community by making Tars available.** Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.** Licensed under the BSD 3-Clause License (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at** https://opensource.org/licenses/BSD-3-Clause** Unless required by applicable law or agreed to in writing, software distributed * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License.*/#ifndef _TC_LOCK_H
#define _TC_LOCK_H#include <string>
#include <stdexcept>
#include <cerrno>
#include "util/tc_ex.h"using namespace std;namespace tars
{
/
/*** @file tc_lock.h * @brief  锁类 */           
//**
* @brief  锁异常
*/
struct TC_Lock_Exception : public TC_Exception
{TC_Lock_Exception(const string &buffer) : TC_Exception(buffer){};TC_Lock_Exception(const string &buffer, int err) : TC_Exception(buffer, err){};~TC_Lock_Exception() throw() {};
};/*** @brief  锁模板类其他具体锁配合使用,* 构造时候加锁,析够的时候解锁*/
template <typename T>
class TC_LockT
{
public:/*** @brief  构造函数,构造时枷锁*  * @param mutex 锁对象*/TC_LockT(const T& mutex) : _mutex(mutex){_mutex.lock();_acquired = true;}/*** @brief  析构,析构时解锁*/virtual ~TC_LockT(){if (_acquired){_mutex.unlock();}}/*** @brief  上锁, 如果已经上锁,则抛出异常*/void acquire() const{if (_acquired){throw TC_Lock_Exception("thread has locked!");}_mutex.lock();_acquired = true;}/*** @brief  尝试上锁.** @return  成功返回true,否则返回false*/bool tryAcquire() const{_acquired = _mutex.tryLock();return _acquired;}/*** @brief  释放锁, 如果没有上过锁, 则抛出异常*/void release() const{if (!_acquired){throw TC_Lock_Exception("thread hasn't been locked!");}_mutex.unlock();_acquired = false;}/*** @brief  是否已经上锁.** @return  返回true已经上锁,否则返回false*/bool acquired() const{return _acquired;}protected:/*** @brief 构造函数* 用于锁尝试操作,与TC_LockT相似*  */TC_LockT(const T& mutex, bool) : _mutex(mutex){_acquired = _mutex.tryLock();}private:// Not implemented; prevents accidental use.TC_LockT(const TC_LockT&);TC_LockT& operator=(const TC_LockT&);protected:/*** 锁对象*/const T&        _mutex;/*** 是否已经上锁*/mutable bool _acquired;
};/*** @brief  尝试上锁*/
template <typename T>
class TC_TryLockT : public TC_LockT<T>
{
public:TC_TryLockT(const T& mutex) : TC_LockT<T>(mutex, true){}
};/*** @brief  空锁, 不做任何锁动作*/
class TC_EmptyMutex
{
public:/*** @brief  写锁.*  * @return int, 0 正确*/int lock()  const   {return 0;}/*** @brief  解写锁*/int unlock() const  {return 0;}/*** @brief  尝试解锁. *  * @return int, 0 正确*/bool trylock() const {return true;}
};/*** @brief  读写锁读锁模板类* 构造时候加锁,析够的时候解锁*/template <typename T>
class TC_RW_RLockT
{
public:/*** @brief  构造函数,构造时枷锁** @param lock 锁对象*/TC_RW_RLockT(T& lock): _rwLock(lock),_acquired(false){_rwLock.ReadLock();_acquired = true;}/*** @brief 析构时解锁*/~TC_RW_RLockT(){if (_acquired){_rwLock.Unlock();}}
private:/***锁对象*/const T& _rwLock;/*** 是否已经上锁*/mutable bool _acquired;TC_RW_RLockT(const TC_RW_RLockT&);TC_RW_RLockT& operator=(const TC_RW_RLockT&);
};template <typename T>
class TC_RW_WLockT
{
public:/*** @brief  构造函数,构造时枷锁** @param lock 锁对象*/TC_RW_WLockT(T& lock): _rwLock(lock),_acquired(false){_rwLock.WriteLock();_acquired = true;}/*** @brief 析构时解锁*/~TC_RW_WLockT(){if(_acquired){_rwLock.Unlock();}}
private:/***锁对象*/const T& _rwLock;/*** 是否已经上锁*/mutable bool _acquired;TC_RW_WLockT(const TC_RW_WLockT&);TC_RW_WLockT& operator=(const TC_RW_WLockT&);
};};
#endif

      针对锁和基本操作,定义了类模板, 仅此而已。 

      这种代码, 我是很欣赏的:

private:// Not implemented; prevents accidental use.TC_LockT(const TC_LockT&);TC_LockT& operator=(const TC_LockT&);

       不多说。

 

 

 

 

这篇关于tars源码漫谈第24篇------tc_lock.h(基本锁)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/684113

相关文章

用js控制视频播放进度基本示例代码

《用js控制视频播放进度基本示例代码》写前端的时候,很多的时候是需要支持要网页视频播放的功能,下面这篇文章主要给大家介绍了关于用js控制视频播放进度的相关资料,文中通过代码介绍的非常详细,需要的朋友可... 目录前言html部分:JavaScript部分:注意:总结前言在javascript中控制视频播放

Python实现无痛修改第三方库源码的方法详解

《Python实现无痛修改第三方库源码的方法详解》很多时候,我们下载的第三方库是不会有需求不满足的情况,但也有极少的情况,第三方库没有兼顾到需求,本文将介绍几个修改源码的操作,大家可以根据需求进行选择... 目录需求不符合模拟示例 1. 修改源文件2. 继承修改3. 猴子补丁4. 追踪局部变量需求不符合很

SpringBoot整合MybatisPlus的基本应用指南

《SpringBoot整合MybatisPlus的基本应用指南》MyBatis-Plus,简称MP,是一个MyBatis的增强工具,在MyBatis的基础上只做增强不做改变,下面小编就来和大家介绍一下... 目录一、MyBATisPlus简介二、SpringBoot整合MybatisPlus1、创建数据库和

Spring 中 BeanFactoryPostProcessor 的作用和示例源码分析

《Spring中BeanFactoryPostProcessor的作用和示例源码分析》Spring的BeanFactoryPostProcessor是容器初始化的扩展接口,允许在Bean实例化前... 目录一、概览1. 核心定位2. 核心功能详解3. 关键特性二、Spring 内置的 BeanFactory

MySQL的隐式锁(Implicit Lock)原理实现

《MySQL的隐式锁(ImplicitLock)原理实现》MySQL的InnoDB存储引擎中隐式锁是一种自动管理的锁,用于保证事务在行级别操作时的数据一致性和安全性,本文主要介绍了MySQL的隐式锁... 目录1. 背景:什么是隐式锁?2. 隐式锁的工作原理3. 隐式锁的类型4. 隐式锁的实现与源代码分析4

MySQL中Next-Key Lock底层原理实现

《MySQL中Next-KeyLock底层原理实现》Next-KeyLock是MySQLInnoDB存储引擎中的一种锁机制,结合记录锁和间隙锁,用于高效并发控制并避免幻读,本文主要介绍了MySQL中... 目录一、Next-Key Lock 的定义与作用二、底层原理三、源代码解析四、总结Next-Key L

Python中多线程和多进程的基本用法详解

《Python中多线程和多进程的基本用法详解》这篇文章介绍了Python中多线程和多进程的相关知识,包括并发编程的优势,多线程和多进程的概念、适用场景、示例代码,线程池和进程池的使用,以及如何选择合适... 目录引言一、并发编程的主要优势二、python的多线程(Threading)1. 什么是多线程?2.

MyBatis-Flex BaseMapper的接口基本用法小结

《MyBatis-FlexBaseMapper的接口基本用法小结》本文主要介绍了MyBatis-FlexBaseMapper的接口基本用法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具... 目录MyBATis-Flex简单介绍特性基础方法INSERT① insert② insertSelec

JAVA调用Deepseek的api完成基本对话简单代码示例

《JAVA调用Deepseek的api完成基本对话简单代码示例》:本文主要介绍JAVA调用Deepseek的api完成基本对话的相关资料,文中详细讲解了如何获取DeepSeekAPI密钥、添加H... 获取API密钥首先,从DeepSeek平台获取API密钥,用于身份验证。添加HTTP客户端依赖使用Jav

C++中使用vector存储并遍历数据的基本步骤

《C++中使用vector存储并遍历数据的基本步骤》C++标准模板库(STL)提供了多种容器类型,包括顺序容器、关联容器、无序关联容器和容器适配器,每种容器都有其特定的用途和特性,:本文主要介绍C... 目录(1)容器及简要描述‌php顺序容器‌‌关联容器‌‌无序关联容器‌(基于哈希表):‌容器适配器‌:(