lock类

作者: 老练子丶2017 | 来源:发表于2017-11-15 21:57 被阅读0次

    锁接口类继承不可拷贝类

    范围锁继承不可拷贝类,里面有继承锁接口类指针,这是什么设计模式呢?

    lock.h:

    #ifndef _LOCK_H

    #define _LOCK_H

    #include <pthread.h>

    #include "baseclass.h"

    //锁接口类

    class ILock : public NonCopy

    {

    public:

    virtual void Lock()=0;

    virtual bool TryLock()=0;

    virtual void UnLock()=0;

    };

    //利用作用域范围自动析构的锁

    class ScopeLock : public NonCopy

    {

    public:

    explicit ScopeLock(ILock *lock) : lock_(lock) {

    lock_->Lock();

    }

    ~ScopeLock() {

    lock_->UnLock();

    }

    private:

    ILock* lock_;

    };

    //自旋锁

    //share_process 是否进程共享  如果共享,锁放在共享内存中

    class SpinLock : public ILock

    {

    public:

    explicit SpinLock(bool share_process=false);

    ~SpinLock();

    virtual void Lock();

    virtual bool TryLock();

    virtual void UnLock();

    private:

    pthread_spinlock_t lock_;

    };

    //互斥锁

    //mutex_recursive 是否可线程递归加锁 true则对一个锁可以上锁多次,解锁只需1次

    class MutexLock : public ILock

    {

    public:

    explicit MutexLock(bool mutex_recursive=false, bool share_process=false);

    ~MutexLock();

    virtual void Lock();

    virtual bool TryLock();

    virtual void UnLock();

    private:

    pthread_mutex_t lock_;

    };

    class RWLockHelper

    {

    public:

    enum __lock_state {

    kUnlock = 0,

    kRdlock = 1,

    kWrlock = 2

    };

    public:

    RWLockHelper(pthread_rwlock_t *rwlock, int state);

    ~RWLockHelper();

    int State() const {

    return state_;

    }

    int Error() const {

    return error_;

    }

    private:

    pthread_rwlock_t *rwlock_;

    int state_;

    int error_;

    };

    #endif // _LOCK_H

    lock.cpp

    #include "lock.h"

    SpinLock::SpinLock(bool share_process) {

    if (share_process) {

    pthread_spin_init(&lock_, PTHREAD_PROCESS_SHARED);

    } else {

    pthread_spin_init(&lock_, PTHREAD_PROCESS_PRIVATE);

    }

    }

    SpinLock::~SpinLock() {

    pthread_spin_destroy(&lock_);

    }

    void SpinLock::Lock() {

    pthread_spin_lock(&lock_);

    }

    bool SpinLock::TryLock() {

    return pthread_spin_trylock(&lock_) == 0;

    }

    void SpinLock::UnLock() {

    pthread_spin_unlock(&lock_);

    }

    MutexLock::MutexLock(bool mutex_recursive, bool share_process) {

    pthread_mutexattr_t attr;

    pthread_mutexattr_init(&attr);

    if (mutex_recursive)

    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);

    if (share_process)

    pthread_mutexattr_settype(&attr, PTHREAD_PROCESS_SHARED);

    pthread_mutex_init(&lock_, &attr);

    pthread_mutexattr_destroy(&attr);

    }

    MutexLock::~MutexLock() {

    pthread_mutex_destroy(&lock_);

    }

    void MutexLock::Lock() {

    pthread_mutex_lock(&lock_);

    }

    bool MutexLock::TryLock() {

    return pthread_mutex_trylock(&lock_) == 0;

    }

    void MutexLock::UnLock() {

    pthread_mutex_unlock(&lock_);

    }

    RWLockHelper::RWLockHelper(pthread_rwlock_t *rwlock, int state)

    : rwlock_(rwlock), state_(state), error_(0) {

    if (state_ == kRdlock)

    error_ = pthread_rwlock_rdlock(rwlock_);

    else if (state_ == kWrlock)

    error_ = pthread_rwlock_wrlock(rwlock_);

    }

    RWLockHelper::~RWLockHelper() {

    pthread_rwlock_unlock(rwlock_);

    state_ = kUnlock;

    error_ = 0;

    }

    int main()

    {

    MutexLock mLock;

    {

    ScopeLock scopeLock(&mLock);

    }

    return 0;

    }

    编译:g++ -o lock lock.cpp -lpthread

    相关文章

      网友评论

        本文标题:lock类

        本文链接:https://www.haomeiwen.com/subject/qexkvxtx.html