作用域互斥锁 [英] Scoped mutex lock

查看:296
本文介绍了作用域互斥锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前从来没有真正使用互斥体,但我需要控制对受保护资源的访问。通过新的C ++ 11的东西,我煮了这个类:

I never really worked with mutexes before, but i need to control access to protected resources. Looking through the new C++11 stuff, i cooked up this class:

class CMutex
{
public:
    class Lockable
    {
        friend class CMutex;
        std::atomic_flag flag;
    public:
        Lockable()
        {
            flag.clear();
        }
    };
private:
    Lockable * resource;
    CMutex(const CMutex &);
public:
    CMutex(Lockable * l)
    {
        resource = l;
        acquire(l);
    }
    CMutex(Lockable & l)
    {
        resource = &l;
        acquire(l);
    }
    CMutex()
        : resource(nullptr)
    {
    }
    ~CMutex()
    {
        if (resource)
            release(resource);
    }
    void acquire(Lockable * l)
    {
        if (!resource)
            resource = l;
        if (!spinLock(2000, resource))
            //explode here
            return;
    }
    void acquire(Lockable & l)
    {
        acquire(&l);
    }
private:
    void release(Lockable * l)
    {
        if (l)
            l->flag.clear();

    }
    static bool spinLock(int ms, Lockable *  bVal)
    {
        using namespace Misc;
        long start;
        int ret;
    loop:
        start = QuickTime();
        while (bVal->flag.test_and_set()) {
            if ((QuickTime() - start) > ms)
                goto time_out;
            // yield thread
            Delay(0);
        }
        // normal exitpoint
        return true;
        // deadlock occurs
    time_out:
        // handle error ...
    }
}

使用方法如下:

class MyClass : public CMutex::lockable
{
    ...
    void doStuff()
    {
        // lock data
        CMutex(this);

        // do stuff
        ...

        // mutex should automagically be RAII released here
    }
    ...
};

首先,我感兴趣的是这个概念是否真的如何应该of std :: atomic等)?

First of all, I'm interested in whether this concept actually works how it should (given the implementation of std::atomic etc.)?

其次,我注意到它正确获取锁,但是它立即释放。我想我应该给锁一个名字?

Secondly, I noticed that it correctly obtains the lock, however it releases it instantly. I guess i should give the lock a name?

CMutex lock(this);

然而,不是编译器在作为优化提供范围之前自由销毁对象它可以保证我不会与对象进行更多的交互?这将破坏这个结构的目的,如果我不能保证析构函数只会在范围退出调用。

However, isn't the compiler free to destruct the object before the scope is left as an optimization provided it can guarantee that i wont interact more with the object? This would defeat the purpose of this construct, if i can't guarantee that the destructor only will be called at scope exit.

注意

推荐答案

不,编译器不能在范围结束前自由破坏。

No, the compiler is not free to destruct before the scope ends.

第12.4节/ 10
- 对于具有自动存储持续时间(3.7.3)的构造对象,当创建对象的块退出时。

Per the C++ Standard section 12.4/10 — for constructed objects with automatic storage duration (3.7.3) when the block in which an object is created exit.

这篇关于作用域互斥锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆