<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">This is very interesting, thanks. Learned a new thing today. I was also not aware of <span class="Apple-style-span" style="font-family: monospace; white-space: pre; ">sched_setaffinity()</span><div>this is actually something I always wondered existed, but had never seen before.</div><div>Thanks a lot again</div><div><br></div><div>Victor</div><div><span class="Apple-style-span" style="font-family: monospace; white-space: pre; "><span class="Apple-style-span" style="font-family: Helvetica; white-space: normal; "> </span></span><br><div><div>On 12 Dec 2009, at 17:47, Gabriel M. Beddingfield wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div><br>Recently on this list, Paul referred to an "atomic integer swap" and an "atomic pointer swap." This was a new concept to me (and possibly others), and this e-mail shares what I've learned.<br><br>If you access a variable from multiple threads -- even a built-in variable like 'int', it is important to control access to the variable with a mutex, semaphore, or an atomic operation.<br><br>Alexander Sandler, on his blog, wrote a couple of good articles on the subject:<br><br> "Do you need a mutex to protect an int?"<br> <a href="http://www.alexonlinux.com/do-you-need-mutex-to-protect-int">http://www.alexonlinux.com/do-you-need-mutex-to-protect-int</a><br><br> "Multithreaded simple data type access and atomic<br> variables"<br> <a href="http://www.alexonlinux.com/multithreaded-simple-data-type-access-and-atomic-variables">http://www.alexonlinux.com/multithreaded-simple-data-type-access-and-atomic-variables</a><br><br>The first article contains code that calculates a wrong answer on multiprocessor machines. I've attached a similar example that will even fail on a single-processor machine.<br><br>There is a wealth of reading material on using Mutexes and Semaphores. However, information on atomic operations appears to be sparse and hard-to-follow. So, here's what I've found:<br><br> + At the moment, there is no built-in support in<br> C/C++ for atomic operations. You will need to use<br> a library, compiler extension, or write your own<br> in assembly code.<br><br> + The GCC compiler has the built-in __sync_*()<br> functions[1] that provide atomic operations.<br> Note that the attached example is using this.<br><br> + glib provides the g_atomic_*() functions[2].<br><br> + Qt 4 has the q_atomic_*() functions.[3] While<br> they are accessible, they are /not/ a part of<br> their stable, public API.<br><br> + The next version of ISO C++ (code name c++0x)<br> is expected to have support for atomic operations<br> (E.g. the std::atomic<T> template) and memory<br> barriers. It may even require that all built-in<br> types be atomic.<br><br> + In the x86 instruction set, these are usually<br> implemented using the 'LOCK' instruction prefix.[5]<br><br>When using atomic operations, perhaps the best advice I found is near the end of Sandler's second article:<br><br> "When using atomic variables, some extra<br> precautions have to be taken.... There is nothing<br> that prevents you from incrementing value of the<br> atomic variable with __sync_fetch_and_add() as I<br> just demonstrated and later in the code doing same<br> thing with regular ++ operator.<br><br> "To address this problem, I strongly suggest<br> wrapping around atomic functions and variables with<br> either ADT in C or C++ class."[4]<br><br>Peace,<br>Gabriel<br><br>[1] <a href="http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html">http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html</a><br>[2] <a href="http://www.gtk.org/api/2.6/glib/glib-Atomic-Operations.html">http://www.gtk.org/api/2.6/glib/glib-Atomic-Operations.html</a><br>[3] <a href="http://doc.trolltech.com/4.3/atomic-operations.html">http://doc.trolltech.com/4.3/atomic-operations.html</a><br> See also the Qt header file QtCore/qatomic_i386.h, and<br> its brothers.<br>[4] <a href="http://www.alexonlinux.com/multithreaded-simple-data-type-access-and-atomic-variables#precautions">http://www.alexonlinux.com/multithreaded-simple-data-type-access-and-atomic-variables#precautions</a><br>[5] <a href="http://siyobik.info/index.php?module=x86&id=159">http://siyobik.info/index.php?module=x86&id=159</a><span><atomic_fail.c></span>_______________________________________________<br>Linux-audio-dev mailing list<br><a href="mailto:Linux-audio-dev@lists.linuxaudio.org">Linux-audio-dev@lists.linuxaudio.org</a><br><a href="http://lists.linuxaudio.org/mailman/listinfo/linux-audio-dev">http://lists.linuxaudio.org/mailman/listinfo/linux-audio-dev</a><br></div></blockquote></div><br></div></body></html>