LWN.net Logo

Advertisement

Our team patches and enhances the Linux kernel and promotes the adoption of Linux at Oracle. mark.wilkerson @oracle.com

Advertise here

Swap

Swap

Posted Apr 1, 2013 21:18 UTC (Mon) by proski (subscriber, #104)
Parent article: A look at C++14: Papers Part 2

The swap operator looks interesting. I hope it would come to C as well. Perhaps it could be made atomic at least for some types.


(Log in to post comments)

Swap

Posted Apr 2, 2013 9:17 UTC (Tue) by tialaramex (subscriber, #21167) [Link]

I can imagine that there might be platforms on which (especially for larger or more sophisticated native C types like doubles) atomic swap is not practical.

So perhaps C could consider offering swap with no promises of atomicity, plus a set of reserved pre-processor variables which tell us whether atomicity is implemented for specific types. This would let you do

#if !defined(SWAP_UINT64_T_IS_ATOMIC)
#error This program requires an atomic swap for unsigned 64-bit integers but you don't have one. Too bad.
#endif

... or something more useful, but you get the idea.

Swap

Posted Apr 2, 2013 20:12 UTC (Tue) by scottwood (subscriber, #74349) [Link]

If non-atomic swap is a useful addition, why would you want to slow down swaps that don't need to be atomic by making it automatic, even if it's optional for the implementation? Atomic swap should be a specially requested operation (e.g. using compiler intrinsics), just like most other atomic operations.

Swap

Posted Apr 3, 2013 5:05 UTC (Wed) by Ben_P (subscriber, #74247) [Link]

Atomic swaps on doubles (and floats) are made more tricky by the fact very often NaN != NaN.

Swap

Posted Apr 5, 2013 23:18 UTC (Fri) by mathstuf (subscriber, #69389) [Link]

Can't you just treat them as integers of the same size for CAS?

Swap

Posted Apr 2, 2013 20:31 UTC (Tue) by wahern (subscriber, #37304) [Link]

Are there any architectures where there exists an atomic, wait-free swap between two memory locations?

I suspect that they're uncommon if not non-existent.

In any event, presumably such an operation was left out of the stdatomic.h libraries for C11 and C++11 for a reason.

Swap

Posted Apr 3, 2013 11:04 UTC (Wed) by heijo (guest, #88363) [Link]

Haswell CPUs with TSX.

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds