]> git.ipfire.org Git - thirdparty/gcc.git/blob - libitm/doc/c-c++-language-constructs-for-tm.rst
sphinx: add missing trailing newline
[thirdparty/gcc.git] / libitm / doc / c-c++-language-constructs-for-tm.rst
1 ..
2 Copyright 1988-2022 Free Software Foundation, Inc.
3 This is part of the GCC manual.
4 For copying conditions, see the copyright.rst file.
5
6 .. _c-c++-language-constructs-for-tm:
7
8 C/C++ Language Constructs for TM
9 --------------------------------
10
11 Transactions are supported in C++ and C in the form of transaction statements,
12 transaction expressions, and function transactions. In the following example,
13 both ``a`` and ``b`` will be read and the difference will be written to
14 ``c``, all atomically and isolated from other transactions:
15
16 .. code-block:: c++
17
18 __transaction_atomic { c = a - b; }
19
20 Therefore, another thread can use the following code to concurrently update
21 ``b`` without ever causing ``c`` to hold a negative value (and without
22 having to use other synchronization constructs such as locks or C++11
23 atomics):
24
25 .. code-block:: c++
26
27 __transaction_atomic { if (a > b) b++; }
28
29 GCC follows the `Draft
30 Specification of Transactional Language Constructs for C++ (v1.1) <https://sites.google.com/site/tmforcplusplus/>`_ in its
31 implementation of transactions.
32
33 The precise semantics of transactions are defined in terms of the C++11/C11
34 memory model (see the specification). Roughly, transactions provide
35 synchronization guarantees that are similar to what would be guaranteed when
36 using a single global lock as a guard for all transactions. Note that like
37 other synchronization constructs in C/C++, transactions rely on a
38 data-race-free program (e.g., a nontransactional write that is concurrent
39 with a transactional read to the same memory location is a data race).