简体   繁体   English

从GCC原子操作构建的轻量级自旋锁?

[英]Lightweight spinlocks built from GCC atomic operations?

I'd like to minimize synchronization and write lock-free code when possible in a project of mine. 我希望在我的项目中尽可能减少同步并编写无锁代码。 When absolutely necessary I'd love to substitute light-weight spinlocks built from atomic operations for pthread and win32 mutex locks. 当绝对必要时,我喜欢用pthread和win32互斥锁替换原子操作中构建的轻量级自旋锁。 My understanding is that these are system calls underneath and could cause a context switch (which may be unnecessary for very quick critical sections where simply spinning a few times would be preferable). 我的理解是这些是下面的系统调用并且可能导致上下文切换(对于非常快速的关键部分而言,这可能是不必要的,其中简单地旋转几次将是更可取的)。

The atomic operations I'm referring to are well documented here: http://gcc.gnu.org/onlinedocs/gcc-4.4.1/gcc/Atomic-Builtins.html 我所指的原子操作在这里有很好的记录: http//gcc.gnu.org/onlinedocs/gcc-4.4.1/gcc/Atomic-Builtins.html

Here is an example to illustrate what I'm talking about. 这是一个例子来说明我在说什么。 Imagine a RB-tree with multiple readers and writers possible. 想象一下可能有多个读者和作者的RB树。 RBTree::exists() is read-only and thread safe, RBTree::insert() would require exclusive access by a single writer (and no readers) to be safe. RBTree :: exists()是只读且线程安全的,RBTree :: insert()需要单个编写器(而不是读者)的独占访问才是安全的。 Some code: 一些代码:

class IntSetTest
{
private:
    unsigned short lock;
    RBTree<int>* myset;

public:
    // ...

    void add_number(int n)
    {
        // Aquire once locked==false (atomic)
        while (__sync_bool_compare_and_swap(&lock, 0, 0xffff) == false);

        // Perform a thread-unsafe operation on the set
        myset->insert(n);

        // Unlock (atomic)
        __sync_bool_compare_and_swap(&lock, 0xffff, 0);
    }

    bool check_number(int n)
    {
        // Increment once the lock is below 0xffff
        u16 savedlock = lock;
        while (savedlock == 0xffff || __sync_bool_compare_and_swap(&lock, savedlock, savedlock+1) == false)
            savedlock = lock;

        // Perform read-only operation    
        bool exists = tree->exists(n);

        // Decrement
        savedlock = lock;
        while (__sync_bool_compare_and_swap(&lock, savedlock, savedlock-1) == false)
            savedlock = lock;

        return exists;
    }
};

(lets assume it need not be exception-safe) (我们假设它不必是例外安全的)

Is this code indeed thread-safe? 这段代码确实是线程安全的吗? Are there any pros/cons to this idea? 这个想法有利有弊吗? Any advice? 有什么建议? Is the use of spinlocks like this a bad idea if the threads are not truly concurrent? 如果线程不是真正并发的话,使用像这样的自旋锁是个坏主意吗?

Thanks in advance. 提前致谢。 ;) ;)

You need a volatile qualifier on lock , and I would also make it a sig_atomic_t . 你需要一个lockvolatile限定符,我也会把它变成一个sig_atomic_t Without the volatile qualifier, this code: 没有volatile限定符,这段代码:

    u16 savedlock = lock;
    while (savedlock == 0xffff || __sync_bool_compare_and_swap(&lock, savedlock, savedlock+1) == false)
        savedlock = lock;

may not re-read lock when updating savedlock in the body of the while-loop. 在更新while循环体中的savedlock时,可能无法重新读取lock Consider the case that lock is 0xffff. 考虑lock是0xffff的情况。 Then, savedlock will be 0xffff prior to checking the loop condition, so the while condition will short-circuit prior to calling __sync_bool_compare_and_swap . 然后,在检查循环条件之前, savedlock将是0xffff,因此while条件将在调用__sync_bool_compare_and_swap之前__sync_bool_compare_and_swap Since __sync_bool_compare_and_swap wasn't called, the compiler doesn't encounter a memory barrier, so it might reasonably assume that the value of lock hasn't changed underneath you, and avoid re-loading it in savedlock . 由于未调用__sync_bool_compare_and_swap ,编译器不会遇到内存屏障,因此可能会合理地假设lock值未在您下面发生更改,并避免在savedlock重新加载它。

Re: sig_atomic_t , there's a decent discussion here . 回复: sig_atomic_t ,有一个体面的讨论在这里 The same considerations that apply to signal handlers would also apply to threads. 适用于信号处理程序的相同注意事项也适用于线程。

With these changes, I'd guess that your code would be thread-safe. 有了这些更改,我猜你的代码是线程安全的。 I would still recommend using mutexes, though, since you really don't know how long your RB-tree insert will take in the general case (per my previous comments under the question). 不过,我仍然建议使用互斥锁,因为你真的不知道你的RB树插入在一般情况下需要多长时间(根据我之前的评论)。

It may be worth noting that if you're using the Win32 mutexes, that from Vista onwards a thread pool is provided for you. 值得注意的是,如果您使用的是Win32互斥锁,那么从Vista开始,就会为您提供一个线程池。 Depending on what you use the RB tree for, you could replace with that. 根据您使用RB树的内容,您可以替换它。

Also, what you should remember is that atomic operations are not particularly fast. 此外,您应该记住的是原子操作并不是特别快。 Microsoft said they were a couple hundred cycles, each. 微软称他们每个人都有几百个周期。

Rather than trying to "protect" the function in this way, it would likely be much more efficient to simply synchronize the threads, either changing to a SIMD/thread pool approach, or to just use a mutex. 不是试图以这种方式“保护”功能,而是简单地同步线程,更改为SIMD /线程池方法或仅使用互斥锁可能更有效。

But, of course, without seeing your code, I can't really make any more comments. 但是,当然,如果没有看到您的代码,我就无法再发表任何评论了。 The trouble with multithreading is that you have to see someone's whole model to understand it. 多线程的麻烦在于你必须看到某个人的整个模型才能理解它。

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

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