Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You can use the CAS primitive between processes. It is guaranteed to be atomic, which is why semaphores are usually built on top of CAS.

He essentially packs 4 byte strings into integers (assuming 32 bit integers) in an atomic manner. In the strictest sense there are no locks here, but at the end of the day he treats each memory location as its own lock. He also does some busy waiting, which is just a good old spin lock:

    while(!InsertAt(data,insertedAt));
Interesting concept but nothing too new. I would also caution against using this code without understanding it first. I don't see much error checking in there.


I think "lock-free" is a misnomer. If you are using a test-and-set instruction, you have incurred all the performance cost of a lock. While much work has been done recently to improve the performance of instructions like CMPXCHG, you still need to treat them like a UDP broadcast message (avoid if possible).

One case I don't think he handles is if a user does a Lookup() for some value in a slot, then tries to delete that slot. In this case, the wrong value might be deleted if an InsertAt() happens in the meanwhile.


How is CAS guaranteed to be atomic? Is it actually implemented by a machine instruction, rather than the pseudocode in the article?


Yes, look up cmpxchg and friends.

FreeBSD has atomic.h providing a bunch of different atomic ops, e.g: http://www.freebsd.org/cgi/cvsweb.cgi/src/sys/amd64/include/...

I believe FreeBSD also uses this style of buffer for dmesg, which sadly also results in a lot of interlacing if two kernel threads are trying to write to the buffer at once.


Yes, it is implemented by the hardware. In x86 I believe it is the CMPXCHG assembler instruction.

GCC, for example has __sync_val_compare_and_swap(...) function that give you access to the hardware compare and swap.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: