基础能力-加解锁

关于加解锁的两种实现方式

方案一:使用C++11 atomic_flag原子特性实现细粒度加解锁
#ifndef YAGER_MUTEX_H
#define YAGER_MUTEX_H

#include <atomic>

class CYagerMutex
{
public:
    CYagerMutex()
        : m_atomicFlag(ATOMIC_FLAG_INIT)
    {}

    ~CYagerMutex()
    {}

    void Lock()
    {
        while (m_atomicFlag.test_and_set());
    }

    void UnLock()
    {
        m_atomicFlag.clear();
    }

private:
    std::atomic_flag  m_atomicFlag;

};
#endif //YAGER_MUTEX_H
方案二:使用libuv的mutex,实现粗粒度的加解锁
#include "libuv/include/uv.h"

class CYagerMutex
{
public:
    CYagerMutex()
    {
        uv_mutex_init(&m_mutex);
    }

    ~CYagerMutex()
    {
        uv_mutex_destroy(&m_mutex);
    }

    void Lock()
    {
        uv_mutex_lock(&m_mutex);
    }

    void UnLock()
    {
        uv_mutex_unlock(&m_mutex);
    }

private:
    uv_mutex_t	m_mutex;

};

关于加解锁的两种实现方式

方案一:使用C++11 atomic_flag原子特性实现细粒度加解锁
#ifndef YAGER_MUTEX_H
#define YAGER_MUTEX_H

#include <atomic>

class CYagerMutex
{
public:
    CYagerMutex()
        : m_atomicFlag(ATOMIC_FLAG_INIT)
    {}

    ~CYagerMutex()
    {}

    void Lock()
    {
        while (m_atomicFlag.test_and_set());
    }

    void UnLock()
    {
        m_atomicFlag.clear();
    }

private:
    std::atomic_flag  m_atomicFlag;

};
#endif //YAGER_MUTEX_H
方案二:使用libuv的mutex,实现粗粒度的加解锁
#include "libuv/include/uv.h"

class CYagerMutex
{
public:
    CYagerMutex()
    {
        uv_mutex_init(&m_mutex);
    }

    ~CYagerMutex()
    {
        uv_mutex_destroy(&m_mutex);
    }

    void Lock()
    {
        uv_mutex_lock(&m_mutex);
    }

    void UnLock()
    {
        uv_mutex_unlock(&m_mutex);
    }

private:
    uv_mutex_t	m_mutex;

};
上一篇:VLC播放器调试经验总结


下一篇:VLC调试:增加messages.c日志函数,在无vlc_object_t下打印日志