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

相关文章

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顺序容器‌‌关联容器‌‌无序关联容器‌(基于哈希表):‌容器适配器‌:(

Go中sync.Once源码的深度讲解

《Go中sync.Once源码的深度讲解》sync.Once是Go语言标准库中的一个同步原语,用于确保某个操作只执行一次,本文将从源码出发为大家详细介绍一下sync.Once的具体使用,x希望对大家有... 目录概念简单示例源码解读总结概念sync.Once是Go语言标准库中的一个同步原语,用于确保某个操

使用Python进行文件读写操作的基本方法

《使用Python进行文件读写操作的基本方法》今天的内容来介绍Python中进行文件读写操作的方法,这在学习Python时是必不可少的技术点,希望可以帮助到正在学习python的小伙伴,以下是Pyth... 目录一、文件读取:二、文件写入:三、文件追加:四、文件读写的二进制模式:五、使用 json 模块读写

Java汇编源码如何查看环境搭建

《Java汇编源码如何查看环境搭建》:本文主要介绍如何在IntelliJIDEA开发环境中搭建字节码和汇编环境,以便更好地进行代码调优和JVM学习,首先,介绍了如何配置IntelliJIDEA以方... 目录一、简介二、在IDEA开发环境中搭建汇编环境2.1 在IDEA中搭建字节码查看环境2.1.1 搭建步

基本知识点

1、c++的输入加上ios::sync_with_stdio(false);  等价于 c的输入,读取速度会加快(但是在字符串的题里面和容易出现问题) 2、lower_bound()和upper_bound() iterator lower_bound( const key_type &key ): 返回一个迭代器,指向键值>= key的第一个元素。 iterator upper_bou

JAVA智听未来一站式有声阅读平台听书系统小程序源码

智听未来,一站式有声阅读平台听书系统 🌟&nbsp;开篇:遇见未来,从“智听”开始 在这个快节奏的时代,你是否渴望在忙碌的间隙,找到一片属于自己的宁静角落?是否梦想着能随时随地,沉浸在知识的海洋,或是故事的奇幻世界里?今天,就让我带你一起探索“智听未来”——这一站式有声阅读平台听书系统,它正悄悄改变着我们的阅读方式,让未来触手可及! 📚&nbsp;第一站:海量资源,应有尽有 走进“智听

【IPV6从入门到起飞】5-1 IPV6+Home Assistant(搭建基本环境)

【IPV6从入门到起飞】5-1 IPV6+Home Assistant #搭建基本环境 1 背景2 docker下载 hass3 创建容器4 浏览器访问 hass5 手机APP远程访问hass6 更多玩法 1 背景 既然电脑可以IPV6入站,手机流量可以访问IPV6网络的服务,为什么不在电脑搭建Home Assistant(hass),来控制你的设备呢?@智能家居 @万物互联

Java ArrayList扩容机制 (源码解读)

结论:初始长度为10,若所需长度小于1.5倍原长度,则按照1.5倍扩容。若不够用则按照所需长度扩容。 一. 明确类内部重要变量含义         1:数组默认长度         2:这是一个共享的空数组实例,用于明确创建长度为0时的ArrayList ,比如通过 new ArrayList<>(0),ArrayList 内部的数组 elementData 会指向这个 EMPTY_EL