Subj : Re: compare and swap operation To : comp.programming.threads From : steve Date : Tue Oct 11 2005 08:38 pm In article , Yang Zhang wrote: >I am reading some lock-free shared data-structures currently and am >planning to implement some of them. They are mostly built on the atomic >CAS (compare and swap) operation. I am just wondering how can I use the >hardware CAS instruction? I googled for some time, but it seems there is >very little information on this. I am mainly using C++, pthread library >and the GNU g++ compiler and how can I use the hardware CAS instruction >on Intel/Linux, Sparc/Solaris, and MIPS/IRIX systems. Can someone >provide some examples or point me to the right place? Thanks in advance. If you are going to be doing lock-free programming, you will need to understand that the primitives involved are not portable between different CPU architectures. For example, MIPS systems don't have CAS, but rather a load-linked/conditional-store instruction pair. It's been long enough since I touched a SPARC that I've now forgotten what synch primitives are available there. You will need to concern yourself with what order memory accesses become visible to other processors, and those details change with each architecture (and often within implementations of an architecture!). You are entering an area that's surprisingly complex. Unless you have compelling evidence that you need lock-free techniques, I would suggest avoiding them at the application level, and using the primitives the system provides. If you plan on writing an API layer for your lock-free queues (or whatever), there's a couple of other recent threads (pun not intended) in this newsgroup that might get you started. -- Steve Watt KD6GGD PP-ASEL-IA ICBM: 121W 56' 57.8" / 37N 20' 14.9" Internet: steve @ Watt.COM Whois: SW32 Free time? There's no such thing. It just comes in varying prices... .