Note that there are some explanatory texts on larger screens.

plurals
  1. POLightweight spinlocks built from GCC atomic operations?
    primarykey
    data
    text
    <p>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. 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).</p> <p>The atomic operations I'm referring to are well documented here: <a href="http://gcc.gnu.org/onlinedocs/gcc-4.4.1/gcc/Atomic-Builtins.html" rel="noreferrer">http://gcc.gnu.org/onlinedocs/gcc-4.4.1/gcc/Atomic-Builtins.html</a></p> <p>Here is an example to illustrate what I'm talking about. Imagine a RB-tree with multiple readers and writers possible. RBTree::exists() is read-only and thread safe, RBTree::insert() would require exclusive access by a single writer (and no readers) to be safe. Some code:</p> <pre><code>class IntSetTest { private: unsigned short lock; RBTree&lt;int&gt;* myset; public: // ... void add_number(int n) { // Aquire once locked==false (atomic) while (__sync_bool_compare_and_swap(&amp;lock, 0, 0xffff) == false); // Perform a thread-unsafe operation on the set myset-&gt;insert(n); // Unlock (atomic) __sync_bool_compare_and_swap(&amp;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(&amp;lock, savedlock, savedlock+1) == false) savedlock = lock; // Perform read-only operation bool exists = tree-&gt;exists(n); // Decrement savedlock = lock; while (__sync_bool_compare_and_swap(&amp;lock, savedlock, savedlock-1) == false) savedlock = lock; return exists; } }; </code></pre> <p>(lets assume it need not be exception-safe)</p> <p>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?</p> <p>Thanks in advance. ;) </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COThe answer I gave in a similar question, http://stackoverflow.com/questions/1919135/critical-sections-that-spin-on-posix/1923218#1923218 , will probably be relevant here.
      singulars
    2. COYour answer was definitely relevant to the issue of using spinlocks in general. They seem like such a good idea for smp machines in they typical case. Would the worst-case situation (a writer that stops running during the critical section) even out with the more likely case of two concurrent threads trying to insert at the same time? What about in a hybrid threading environment where user threads are mapped onto a number of kernel threads equal to the number of logical processors on the machine? The worst-case situation would be even less likely then; no?
      singulars
    3. COI'm not sure the extent to which the number of kernel threads affects the likelihood of running into performance issues. It's possible that the writer-thread has just used up its time-slice between the entry and exit of the lock, which would lead to the problem case no matter how many kernel threads there are. On this point, I'll note that the RB-tree insert operation is O(log(n)), so the larger the tree, the more likely this problem is to occur. Also, a larger tree is more likely to cause page-faults during update, which would also make the problem case more likely. I'd avoid spinlocks here.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload