tars源码漫谈第25篇------tc_thread_rwlock.h/tc_thread_rwlock.cpp(读写锁)

      tc_thread_rwlock里面的类TC_ThreadRWLocker是对读写锁操作的封装。 而读写锁, 是linux中非常基本的概念, 在某次面试的时候, 我还被问到过。

      来看看:

/**
 * 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.
 */

#include "util/tc_thread_rwlock.h"
#include <string.h>
#include <iostream>
#include <cassert>

namespace tars
{

TC_ThreadRWLocker::TC_ThreadRWLocker()
{
    int ret = ::pthread_rwlock_init(&m_sect, NULL);
    assert(ret == 0);

    if(ret != 0)
    {
        throw TC_ThreadRW_Exception("[TC_ThreadRWLocker::TC_ThreadRWLocker] pthread_rwlock_init error", ret);
    }
}

TC_ThreadRWLocker::~TC_ThreadRWLocker()
{
    int ret = ::pthread_rwlock_destroy(&m_sect);
    if(ret != 0)
    {
        cerr<<"[TC_ThreadRWLocker::~TC_ThreadRWLocker] pthread_rwlock_destroy error:"<<string(strerror(ret))<<endl;;
    }

}

void TC_ThreadRWLocker::ReadLock() const
{
    int ret = ::pthread_rwlock_rdlock(&m_sect);
    if(ret != 0)
    {
        if(ret == EDEADLK)
        {
            throw TC_ThreadRW_Exception("[TC_ThreadRWLocker::ReadLock] pthread_rwlock_rdlock dead lock error", ret);
        }
        else
        {
            throw TC_ThreadRW_Exception("[TC_ThreadRWLocker::ReadLock] pthread_rwlock_rdlock error", ret);
        }
    }
}


void TC_ThreadRWLocker::WriteLock() const
{
    int ret = ::pthread_rwlock_wrlock(&m_sect);
    if(ret != 0)
    {
        if(ret == EDEADLK)
        {
            throw TC_ThreadRW_Exception("[TC_ThreadRWLocker::WriteLock] pthread_rwlock_wrlock dead lock error", ret);
        }
        else
        {
            throw TC_ThreadRW_Exception("[TC_ThreadRWLocker::WriteLock] pthread_rwlock_wrlock error", ret);
        }
    }

}

void TC_ThreadRWLocker::TryReadLock() const
{
    int ret = ::pthread_rwlock_tryrdlock(&m_sect);
    if(ret != 0)
    {
        if(ret == EDEADLK)
        {
            throw TC_ThreadRW_Exception("[TC_ThreadRWLocker::TryReadLock] pthread_rwlock_tryrdlock dead lock error", ret);
        }
        else
        {
            throw TC_ThreadRW_Exception("[TC_ThreadRWLocker::TryReadLock] pthread_rwlock_tryrdlock error", ret);
        }
    }

}

void TC_ThreadRWLocker::TryWriteLock() const
{
    int ret = ::pthread_rwlock_trywrlock(&m_sect);
    if(ret != 0)
    {
        if(ret == EDEADLK)
        {
            throw TC_ThreadRW_Exception("[TC_ThreadRWLocker::TryWriteLock] pthread_rwlock_trywrlock dead lock error", ret);
        }
        else
        {
            throw TC_ThreadRW_Exception("[TC_ThreadRWLocker::TryWriteLock] pthread_rwlock_trywrlock error", ret);
        }
    }

}

void TC_ThreadRWLocker::Unlock() const
{
    int ret = ::pthread_rwlock_unlock(&m_sect);
    if(ret != 0)
    {
        throw TC_ThreadRW_Exception("[TC_ThreadRWLocker::Unlock] pthread_rwlock_unlock error", ret);
    }

}

}

          可以看到, 是简单的封装, 别无其他。

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值