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

相关文章

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

ModelMapper基本使用和常见场景示例详解

《ModelMapper基本使用和常见场景示例详解》ModelMapper是Java对象映射库,支持自动映射、自定义规则、集合转换及高级配置(如匹配策略、转换器),可集成SpringBoot,减少样板... 目录1. 添加依赖2. 基本用法示例:简单对象映射3. 自定义映射规则4. 集合映射5. 高级配置匹

SQL BETWEEN 语句的基本用法详解

《SQLBETWEEN语句的基本用法详解》SQLBETWEEN语句是一个用于在SQL查询中指定查询条件的重要工具,它允许用户指定一个范围,用于筛选符合特定条件的记录,本文将详细介绍BETWEEN语... 目录概述BETWEEN 语句的基本用法BETWEEN 语句的示例示例 1:查询年龄在 20 到 30 岁

mysql中insert into的基本用法和一些示例

《mysql中insertinto的基本用法和一些示例》INSERTINTO用于向MySQL表插入新行,支持单行/多行及部分列插入,下面给大家介绍mysql中insertinto的基本用法和一些示例... 目录基本语法插入单行数据插入多行数据插入部分列的数据插入默认值注意事项在mysql中,INSERT I

mapstruct中的@Mapper注解的基本用法

《mapstruct中的@Mapper注解的基本用法》在MapStruct中,@Mapper注解是核心注解之一,用于标记一个接口或抽象类为MapStruct的映射器(Mapper),本文给大家介绍ma... 目录1. 基本用法2. 常用属性3. 高级用法4. 注意事项5. 总结6. 编译异常处理在MapSt

MyBatis ResultMap 的基本用法示例详解

《MyBatisResultMap的基本用法示例详解》在MyBatis中,resultMap用于定义数据库查询结果到Java对象属性的映射关系,本文给大家介绍MyBatisResultMap的基本... 目录MyBATis 中的 resultMap1. resultMap 的基本语法2. 简单的 resul

Java 枚举的基本使用方法及实际使用场景

《Java枚举的基本使用方法及实际使用场景》枚举是Java中一种特殊的类,用于定义一组固定的常量,枚举类型提供了更好的类型安全性和可读性,适用于需要定义一组有限且固定的值的场景,本文给大家介绍Jav... 目录一、什么是枚举?二、枚举的基本使用方法定义枚举三、实际使用场景代替常量状态机四、更多用法1.实现接

git stash命令基本用法详解

《gitstash命令基本用法详解》gitstash是Git中一个非常有用的命令,它可以临时保存当前工作区的修改,让你可以切换到其他分支或者处理其他任务,而不需要提交这些还未完成的修改,这篇文章主要... 目录一、基本用法1. 保存当前修改(包括暂存区和工作区的内容)2. 查看保存了哪些 stash3. 恢

解决mysql插入数据锁等待超时报错:Lock wait timeout exceeded;try restarting transaction

《解决mysql插入数据锁等待超时报错:Lockwaittimeoutexceeded;tryrestartingtransaction》:本文主要介绍解决mysql插入数据锁等待超时报... 目录报错信息解决办法1、数据库中执行如下sql2、再到 INNODB_TRX 事务表中查看总结报错信息Lock

MySQL基本查询示例总结

《MySQL基本查询示例总结》:本文主要介绍MySQL基本查询示例总结,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Create插入替换Retrieve(读取)select(确定列)where条件(确定行)null查询order by语句li