简体   繁体   English

范围互斥锁

[英]Scoped mutex lock

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: 查看新的C ++ 11内容,我完成了此类:

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 ...
    }
}

Usage like so: 用法如下:

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

        // do stuff
        ...

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

First of all, I'm interested in whether this concept actually works how it should (given the implementation of std::atomic etc.)? 首先,我对这个概念是否真正起作用感到兴趣(考虑到std :: atomic等的实现)?

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. 如果我不能保证仅在范围出口处调用析构函数,这将破坏此构造的目的。

Regards 问候

No, the compiler is not free to destruct before the scope ends. 不,在范围结束之前,编译器不能随意销毁。

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. 根据C ++标准第12.4 / 10节-当在其中创建对象的块退出时,具有自动存储持续时间(3.7.3)的构造对象。

Here is a trick that may come handy for you and it works with all mainstream (VC++, clang, gcc) compilers: 这是一个技巧,可能适合您,并且适用于所有主流(VC ++,clang和gcc)编译器:

#define APPEND_ID1(id1, id2) id1##id2
#define APPEND_ID2(id1, id2) APPEND_ID1(id1, id2)
#define APPEND_COUNTER(id) APPEND_ID2(id, __COUNTER__)

#define SCOPED_LOCK(lockable) CMutex APPEND_COUNTER(scoped_lock)(lockable)

class MyClass : public CMutex::lockable
{
    ...
    void doStuff()
    {
        // lock data
        SCOPED_LOCK(this);
        // auto-generates a unique name, works even with multiple locks in the same scope..
        SCOPED_LOCK(that);

        // do stuff
        ...

        // mutex should automagically be RAII released here
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM