Using C/C++ Atomics

Introduction

The Intel® C++ Compiler 12.0 available in Intel® Parallel Composer 2011 and Intel® C++ Composer XE now supports the atomic data types.  These standard data types provide a standard way of implementing fine grained atomic locking in C and C++ which can be useful for many different parallel methodologies, from native threads to Intel® Threading Building Blocks to OpenMP* to Intel® Cilk™ Plus.  This article covers the basic syntax and how it works to ensure threadsafe accesses of shared data.

Atomics Requirements

  • Intel® Parallel Composer 2011
  • #include the <atomic> or <stdatomic.h> header files

atomic_flag

The atomic_flag data type is the basis for all atomic data types.  It provides two operations that can be done atomically - a test and set operation, and a clear operation.  The flag is a bool that is set to 0 when clear() is used.  For test_and_set, the current value of the bool is returned and then the flag is set to 1.

The syntax for this is:

std::atomic_flag x;
bool y = x.test_and_set();
x.clear();



Other atomic data types

In short, void* (as atomic_address), bool (atomic_bool), and various char/integer types are supported.  These can also be alternatively declared in C++ using atomic<T> where T is void*, bool, or the char/int type.

atomic_bool supports the following operations:

bool load()
-- Atomically return the value of atomic_bool.  Can also happen when using the "=" assignment operator or during conversions.

void store(bool)
-- Atomically set the value of atomic_bool.  Can also happen when using the "=" assignment operator or during conversions.

bool exchange(bool)
-- Atomically swap the values of the current atomic_bool with the boolean input.  Return the old value of the atomic bool.

bool compare_exchange_weak(bool & expected, bool desired)
-- Atomically: compare the current value of atomic_bool with expected.  If equal, set atomic_bool value to value of desired and return true.  If not equal, set expected to desired value and return false.  Intended for use in loops.

bool compare_exchange_strong(bool & expected, bool desired)
-- Atomically: compare the current value of atomic_bool with expected. If equal, set atomic_bool value to value of desired and return true. If not equal, set expected to desired value and return false. Intended for use outside of loops.

atomic_address supports the above atomic_bool operations for void* types, and also the following:

Operator +=(void*)
-- Atomically increment the atomic_address by the input number of bytes and store

Operator -=(void*)
-- Atomically decrement the atomic_address by the input number of bytes and store

Other atomic types:

The other types that are supported:

  • atomic_char16_t
  • atomic_char32_t
  • atomic_wchar_t
  • atomic_char
  • atomic_schar
  • atomic_uchar
  • atomic_short
  • atomic_ushort
  • atomic_int
  • atomic_uint
  • atomic_long
  • atomic_ulong
  • atomic_llong
  • atomic_ullong


These data types support the atomic_bool and atomic_address functions above for their data type, plus the following:

Operator ++:
-- Atomically increment by 1

Operator --:
-- Atomically decrement by 1

Operator &= (T)
-- Atomically bitwise-and with input and store

Operator |= (T)
-- Atomically bitwise-or with input and store

Operator ^= (T)
-- Atomically bitwise-xor with input and store

Technical Support

Visit our online Community Support User Forums and Knowledge Base to get all of the help you need from our own tools and parallelism experts, and your fellow developers. Go to/en-us/to start your search.



Intel logo

For product and purchase information visit:

http://intel.com/software/products

Intel and the Intel logo, are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

*Other names and brands may be claimed as the property of others.

INFORMATION IN THIS DOCUMENT IS PROVIDED IN CONNECTION WITH INTEL PRODUCTS. NO LICENSE, EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, TO ANY INTELLECTUAL PROPERTY RIGHTS IS GRANTED BY THIS DOCUMENT. EXCEPT AS PROVIDED IN INTEL’S TERMS AND CONDITIONS OF SALE FOR SUCH PRODUCTS, INTEL ASSUMES NO LIABILITY WHATSOEVER, AND INTEL DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY, RELATING TO SALE AND/OR USE OF INTEL PRODUCTS INCLUDING LIABILITY OR WARRANTIES RELATING TO FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT. Intel products are not intended for use in medical, life saving, life sustaining applications. Intel may make changes to specifications and product descriptions at any time, without notice.

Copyright © 2010, Intel Corporation. All Rights Reserved.

 

For more complete information about compiler optimizations, see our Optimization Notice.
Tags: