Talk:cpp/atomic/atomic exchange


The spinlock mutex example is incorrect. You cannot implement a spinlock by using atomic_exchange (which is *not* equivalent to atomic_test_and_set). You need a CAS for the spinlock. It is clear that the example is not doing what is intended, when run.

could you be more specific? In the example, threads spin continuously storing true and reading true until some thread stores false (thereby releasing the implied mutex). Only one thread gets to read false (thereby acquiring the implied mutex) since it atomically stores true in the lock and the other threads continue to spin. Are you expecting different behavior? (also, test-and-set is implemented as atomic exchange on platforms that have the latter but not the former) --Cubbi (talk) 04:50, 12 October 2013 (PDT)