• pthread_wrap.h


    #ifndef _PTHREAD_WRAP_H
    #define _PTHREAD_WRAP_H
    #include <pthread.h>

    class hm_pthread_mutex
    {
    public:
    hm_pthread_mutex()
    {
    pthread_mutex_init(&m_mutex, 0);
    }
    ~hm_pthread_mutex()
    {
    pthread_mutex_destroy(&m_mutex);
    }

    inline void lock()
    {
    pthread_mutex_lock(&m_mutex);
    }
    inline void unlock()
    {
    pthread_mutex_unlock(&m_mutex);
    }

    private:
    pthread_mutex_t m_mutex;
    };

    class hm_pthread_rwlock
    {
    public:
    hm_pthread_rwlock()
    {
    pthread_rwlock_init(&m_rwlock, 0);
    }
    ~hm_pthread_rwlock()
    {
    pthread_rwlock_destroy(&m_rwlock);
    }

    inline void rdlock()
    {
    pthread_rwlock_rdlock(&m_rwlock);
    }
    inline void wrlock()
    {
    pthread_rwlock_wrlock(&m_rwlock);
    }
    inline void unlock()
    {
    pthread_rwlock_unlock(&m_rwlock);
    }

    private:
    pthread_rwlock_t m_rwlock;
    };

    class hm_pthread_cond
    {
    public:
    hm_pthread_cond()
    {
    m_complete = false;
    pthread_mutex_init(&m_mutex, 0);
    pthread_cond_init(&m_cond, 0);
    }

    ~hm_pthread_cond()
    {
    pthread_mutex_destroy(&m_mutex);
    pthread_cond_destroy(&m_cond);
    }

    inline void wait()
    {
    pthread_mutex_lock(&m_mutex);

    m_complete = false;
    while (!m_complete)
    pthread_cond_wait(&m_cond, &m_mutex);

    pthread_mutex_unlock(&m_mutex);
    }

    inline void notify(bool complete=true)
    {
    pthread_mutex_lock(&m_mutex);

    m_complete = complete;
    pthread_cond_signal(&m_cond);

    pthread_mutex_unlock(&m_mutex);
    }

    private:
    bool m_complete;
    pthread_mutex_t m_mutex;
    pthread_cond_t m_cond;
    };

    #endif//_PTHREAD_WRAP_H

  • 相关阅读:
    抽丝剥茧设计模式- 责任链模式应用场景
    抽丝剥茧设计模式-工厂和抽象工厂模式
    抽丝剥茧设计模式-你真的懂单例模式吗?
    抽丝剥茧设计模式- 啰嗦几句
    生产环境ng配置
    Linux命令
    内容过滤报错
    user 报错
    cxlabel 显示不全的解决方法
    关于SQL事务的一些坑
  • 原文地址:https://www.cnblogs.com/walker-lc/p/4036674.html
Copyright © 2020-2023  润新知