// filename:c2011-7-17-7-4-ex.c // original examples and/or notes: // (c) ISO/IEC JTC1 SC22 WG14 N1570, April 12, 2011 // C2011 7.17.7.4 The atomic_compare_exchange generic functions // compile and output mechanism: // (c) Ogawa Kiyoshi, kaizen@gifu-u.ac.jp, December.29, 2013 // compile errors and/or wornings: // (c) Apple LLVM version 4.2 (clang-425.0.27) (based on LLVM 3.2svn) // Target: x86_64-apple-darwin11.4.2 //Thread model: posix // (c) LLVM 2003-2009 University of Illinois at Urbana-Champaign. // gcc-4.9 (GCC) 4.9.0 20131229 (experimental) // Copyright (C) 2013 Free Software Foundation, Inc. #include #include #include char * function ( char * add){ return add;}; int main(void) { char object[] = "\0abc\0de"; char expected[] = "\0abc\0de"; char desired[] = "\0abcdef"; char * exp=object, * cur=expected, * des=desired; if (memcmp(object, expected, sizeof (*object)) == 0) memcpy(object, &desired, sizeof (*object)); else memcpy(expected, object, sizeof (*object)); // Example exp = atomic_load(&cur); do { des = function(exp); } while (!atomic_compare_exchange_weak(&cur, &exp, des)); return printf("7.17.7.4 The atomic_compare_exchange generic functions %d\n", exp); } // output may be // 7.17.7.4 The atomic_compare_exchange generic functions 1476647632