定制 RAII C++作用域互斥锁的实现

发布于 2024-12-13 00:52:31 字数 189 浏览 1 评论 0原文

我无法使用 boost 或最新的 std::thread 库。解决方法是创建作用域互斥体的自定义实现。

简而言之,当一个类实例创建一个互斥锁时。类销毁后,互斥锁被解锁。

有可用的实施吗?我不想重新发明轮子。

我需要使用 pthreads。

  • 资源获取就是初始化==“RAII”

I cannot use boost or the latest std::thread library. The way to go is to create a custom implementation of a scoped mutex.

In a few words when a class instance is create a mutex locks. Upon class destruction the mutex is unlocked.

Any implementation available? I don't want to re-invent the wheel.

I need to use pthreads.

  • resource acquisition is initialization == “RAII”

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撑一把青伞 2024-12-20 00:52:31

注意这是一个旧答案。 C++11 包含更好的助手,更加独立于平台:

以及其他选项,例如 std::unique_lock、boost::unique_lock

任何RAII 教程就可以了。

要点如下:(也在 http://ideone.com/kkB86 上)

// stub mutex_t: implement this for your operating system
struct mutex_t 
{ 
    void Acquire() {} 
    void Release() {} 
};

struct LockGuard
{
     LockGuard(mutex_t& mutex) : _ref(mutex) 
     { 
         _ref.Acquire();  // TODO operating system specific
     }

     ~LockGuard() 
     { 
          _ref.Release(); // TODO operating system specific
     }
   private:
     LockGuard(const LockGuard&); // or use c++0x ` = delete`

     mutex_t& _ref;
};

int main()
{
    mutex_t mtx;

    {
        LockGuard lock(mtx);
        // LockGuard copy(lock); // ERROR: constructor private
        // lock = LockGuard(mtx);// ERROR: no default assignment operator
    }

}

当然你可以将其设为通用对于 mutex_t,您可以防止子类化。
由于引用字段

EDIT,复制/分配已被禁止 对于 pthreads:

struct mutex_t
{
    public:
        mutex_t(pthread_mutex_t &lock) : m_mutex(lock) {}

        void Acquire() { pthread_mutex_lock(&m_mutex);   }
        void Release() { pthread_mutex_unlock(&m_mutex); }
    private:
        pthread_mutex_t& m_mutex;
};

Note This is an old answer. C++11 contains better helpers that are more platform independent:

And other options like std::unique_lock, boost::unique_lock

Any RAII tutorial will do.

Here's the gist: (also on http://ideone.com/kkB86)

// stub mutex_t: implement this for your operating system
struct mutex_t 
{ 
    void Acquire() {} 
    void Release() {} 
};

struct LockGuard
{
     LockGuard(mutex_t& mutex) : _ref(mutex) 
     { 
         _ref.Acquire();  // TODO operating system specific
     }

     ~LockGuard() 
     { 
          _ref.Release(); // TODO operating system specific
     }
   private:
     LockGuard(const LockGuard&); // or use c++0x ` = delete`

     mutex_t& _ref;
};

int main()
{
    mutex_t mtx;

    {
        LockGuard lock(mtx);
        // LockGuard copy(lock); // ERROR: constructor private
        // lock = LockGuard(mtx);// ERROR: no default assignment operator
    }

}

Of course you can make it generic towards mutex_t, you could prevent subclassing.
Copying/assignment is already prohibited because of the reference field

EDIT For pthreads:

struct mutex_t
{
    public:
        mutex_t(pthread_mutex_t &lock) : m_mutex(lock) {}

        void Acquire() { pthread_mutex_lock(&m_mutex);   }
        void Release() { pthread_mutex_unlock(&m_mutex); }
    private:
        pthread_mutex_t& m_mutex;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文