summaryrefslogtreecommitdiff
path: root/doc/html/atomic/interface.html
blob: 6fb86638294fd0448f8ea52a639b989bc1087bfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Programming interfaces</title>
<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="../atomic.html" title="Chapter&#160;8.&#160;Boost.Atomic">
<link rel="prev" href="thread_coordination.html" title="Thread coordination using Boost.Atomic">
<link rel="next" href="usage_examples.html" title="Usage examples">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
<td align="center"><a href="../../../index.html">Home</a></td>
<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="thread_coordination.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../atomic.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="usage_examples.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="atomic.interface"></a><a class="link" href="interface.html" title="Programming interfaces">Programming interfaces</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="interface.html#atomic.interface.configuration">Configuration and building</a></span></dt>
<dt><span class="section"><a href="interface.html#atomic.interface.interface_memory_order">Memory order</a></span></dt>
<dt><span class="section"><a href="interface.html#atomic.interface.interface_atomic_object">Atomic objects</a></span></dt>
<dt><span class="section"><a href="interface.html#atomic.interface.interface_fences">Fences</a></span></dt>
<dt><span class="section"><a href="interface.html#atomic.interface.feature_macros">Feature testing macros</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="atomic.interface.configuration"></a><a class="link" href="interface.html#atomic.interface.configuration" title="Configuration and building">Configuration and building</a>
</h3></div></div></div>
<p>
        The library contains header-only and compiled parts. The library is header-only
        for lock-free cases but requires a separate binary to implement the lock-based
        emulation. Users are able to detect whether linking to the compiled part
        is required by checking the <a class="link" href="interface.html#atomic.interface.feature_macros" title="Feature testing macros">feature
        macros</a>.
      </p>
<p>
        The following macros affect library behavior:
      </p>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                <p>
                  Macro
                </p>
              </th>
<th>
                <p>
                  Description
                </p>
              </th>
</tr></thead>
<tbody>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_NO_CMPXCHG8B</span></code>
                </p>
              </td>
<td>
                <p>
                  Affects 32-bit x86 Oracle Studio builds. When defined, the library
                  assumes the target CPU does not support <code class="computeroutput"><span class="identifier">cmpxchg8b</span></code>
                  instruction used to support 64-bit atomic operations. This is the
                  case with very old CPUs (pre-Pentium). The library does not perform
                  runtime detection of this instruction, so running the code that
                  uses 64-bit atomics on such CPUs will result in crashes, unless
                  this macro is defined. Note that the macro does not affect MSVC,
                  GCC and compatible compilers because the library infers this information
                  from the compiler-defined macros.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_NO_CMPXCHG16B</span></code>
                </p>
              </td>
<td>
                <p>
                  Affects 64-bit x86 MSVC and Oracle Studio builds. When defined,
                  the library assumes the target CPU does not support <code class="computeroutput"><span class="identifier">cmpxchg16b</span></code> instruction used to
                  support 128-bit atomic operations. This is the case with some early
                  64-bit AMD CPUs, all Intel CPUs and current AMD CPUs support this
                  instruction. The library does not perform runtime detection of
                  this instruction, so running the code that uses 128-bit atomics
                  on such CPUs will result in crashes, unless this macro is defined.
                  Note that the macro does not affect GCC and compatible compilers
                  because the library infers this information from the compiler-defined
                  macros.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_NO_MFENCE</span></code>
                </p>
              </td>
<td>
                <p>
                  Affects 32-bit x86 Oracle Studio builds. When defined, the library
                  assumes the target CPU does not support <code class="computeroutput"><span class="identifier">mfence</span></code>
                  instruction used to implement thread fences. This instruction was
                  added with SSE2 instruction set extension, which was available
                  in CPUs since Intel Pentium 4. The library does not perform runtime
                  detection of this instruction, so running the library code on older
                  CPUs will result in crashes, unless this macro is defined. Note
                  that the macro does not affect MSVC, GCC and compatible compilers
                  because the library infers this information from the compiler-defined
                  macros.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_FORCE_FALLBACK</span></code>
                </p>
              </td>
<td>
                <p>
                  When defined, all operations are implemented with locks. This is
                  mostly used for testing and should not be used in real world projects.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_DYN_LINK</span></code>
                  and <code class="computeroutput"><span class="identifier">BOOST_ALL_DYN_LINK</span></code>
                </p>
              </td>
<td>
                <p>
                  Control library linking. If defined, the library assumes dynamic
                  linking, otherwise static. The latter macro affects all Boost libraries,
                  not just <span class="bold"><strong>Boost.Atomic</strong></span>.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_NO_LIB</span></code>
                  and <code class="computeroutput"><span class="identifier">BOOST_ALL_NO_LIB</span></code>
                </p>
              </td>
<td>
                <p>
                  Control library auto-linking on Windows. When defined, disables
                  auto-linking. The latter macro affects all Boost libraries, not
                  just <span class="bold"><strong>Boost.Atomic</strong></span>.
                </p>
              </td>
</tr>
</tbody>
</table></div>
<p>
        Besides macros, it is important to specify the correct compiler options for
        the target CPU. With GCC and compatible compilers this affects whether particular
        atomic operations are lock-free or not.
      </p>
<p>
        Boost building process is described in the <a href="http://www.boost.org/doc/libs/release/more/getting_started/" target="_top">Getting
        Started guide</a>. For example, you can build <span class="bold"><strong>Boost.Atomic</strong></span>
        with the following command line:
      </p>
<pre class="programlisting">bjam --with-atomic variant=release instruction-set=core2 stage
</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="atomic.interface.interface_memory_order"></a><a class="link" href="interface.html#atomic.interface.interface_memory_order" title="Memory order">Memory order</a>
</h3></div></div></div>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">memory_order</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
</pre>
<p>
        The enumeration <code class="literal">boost::memory_order</code> defines the following
        values to represent memory ordering constraints:
      </p>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                <p>
                  Constant
                </p>
              </th>
<th>
                <p>
                  Description
                </p>
              </th>
</tr></thead>
<tbody>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">memory_order_relaxed</span></code>
                </p>
              </td>
<td>
                <p>
                  No ordering constraint. Informally speaking, following operations
                  may be reordered before, preceding operations may be reordered
                  after the atomic operation. This constraint is suitable only when
                  either a) further operations do not depend on the outcome of the
                  atomic operation or b) ordering is enforced through stand-alone
                  <code class="computeroutput"><span class="identifier">atomic_thread_fence</span></code>
                  operations. The operation on the atomic value itself is still atomic
                  though.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">memory_order_release</span></code>
                </p>
              </td>
<td>
                <p>
                  Perform <code class="computeroutput"><span class="identifier">release</span></code>
                  operation. Informally speaking, prevents all preceding memory operations
                  to be reordered past this point.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">memory_order_acquire</span></code>
                </p>
              </td>
<td>
                <p>
                  Perform <code class="computeroutput"><span class="identifier">acquire</span></code>
                  operation. Informally speaking, prevents succeeding memory operations
                  to be reordered before this point.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">memory_order_consume</span></code>
                </p>
              </td>
<td>
                <p>
                  Perform <code class="computeroutput"><span class="identifier">consume</span></code>
                  operation. More relaxed (and on some architectures more efficient)
                  than <code class="computeroutput"><span class="identifier">memory_order_acquire</span></code>
                  as it only affects succeeding operations that are computationally-dependent
                  on the value retrieved from an atomic variable.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">memory_order_acq_rel</span></code>
                </p>
              </td>
<td>
                <p>
                  Perform both <code class="computeroutput"><span class="identifier">release</span></code>
                  and <code class="computeroutput"><span class="identifier">acquire</span></code> operation
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">memory_order_seq_cst</span></code>
                </p>
              </td>
<td>
                <p>
                  Enforce sequential consistency. Implies <code class="computeroutput"><span class="identifier">memory_order_acq_rel</span></code>,
                  but additionally enforces total order for all operations such qualified.
                </p>
              </td>
</tr>
</tbody>
</table></div>
<p>
        See section <a class="link" href="thread_coordination.html" title="Thread coordination using Boost.Atomic"><span class="emphasis"><em>happens-before</em></span></a>
        for explanation of the various ordering constraints.
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="atomic.interface.interface_atomic_object"></a><a class="link" href="interface.html#atomic.interface.interface_atomic_object" title="Atomic objects">Atomic objects</a>
</h3></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="interface.html#atomic.interface.interface_atomic_object.interface_atomic_generic"><code class="literal">boost::atomic&lt;<span class="emphasis"><em>T</em></span>&gt;</code>
        template class</a></span></dt>
<dt><span class="section"><a href="interface.html#atomic.interface.interface_atomic_object.interface_atomic_integral"><code class="literal">boost::atomic&lt;<span class="emphasis"><em>integral</em></span>&gt;</code>
        template class</a></span></dt>
<dt><span class="section"><a href="interface.html#atomic.interface.interface_atomic_object.interface_atomic_pointer"><code class="literal">boost::atomic&lt;<span class="emphasis"><em>pointer</em></span>&gt;</code>
        template class</a></span></dt>
</dl></div>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">atomic</span><span class="special">/</span><span class="identifier">atomic</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
</pre>
<p>
        <code class="literal">boost::atomic&lt;<span class="emphasis"><em>T</em></span>&gt;</code> provides methods
        for atomically accessing variables of a suitable type <code class="literal"><span class="emphasis"><em>T</em></span></code>.
        The type is suitable if it is <span class="emphasis"><em>trivially copyable</em></span> (3.9/9
        [basic.types]). Following are examples of the types compatible with this
        requirement:
      </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
            a scalar type (e.g. integer, boolean, enum or pointer type)
          </li>
<li class="listitem">
            a <code class="literal">class</code> or <code class="literal">struct</code> that has no non-trivial
            copy or move constructors or assignment operators, has a trivial destructor,
            and that is comparable via <code class="literal">memcmp</code>.
          </li>
</ul></div>
<p>
        Note that classes with virtual functions or virtual base classes do not satisfy
        the requirements. Also be warned that structures with "padding"
        between data members may compare non-equal via <code class="literal">memcmp</code>
        even though all members are equal.
      </p>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="atomic.interface.interface_atomic_object.interface_atomic_generic"></a><a class="link" href="interface.html#atomic.interface.interface_atomic_object.interface_atomic_generic" title="boost::atomic&lt;T&gt; template class"><code class="literal">boost::atomic&lt;<span class="emphasis"><em>T</em></span>&gt;</code>
        template class</a>
</h4></div></div></div>
<p>
          All atomic objects supports the following operations:
        </p>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                  <p>
                    Syntax
                  </p>
                </th>
<th>
                  <p>
                    Description
                  </p>
                </th>
</tr></thead>
<tbody>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">atomic</span><span class="special">()</span></code>
                  </p>
                </td>
<td>
                  <p>
                    Initialize to an unspecified value
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">atomic</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">initial_value</span><span class="special">)</span></code>
                  </p>
                </td>
<td>
                  <p>
                    Initialize to <code class="literal">initial_value</code>
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="keyword">bool</span> <span class="identifier">is_lock_free</span><span class="special">()</span></code>
                  </p>
                </td>
<td>
                  <p>
                    Checks if the atomic object is lock-free
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">T</span> <span class="identifier">load</span><span class="special">(</span><span class="identifier">memory_order</span>
                    <span class="identifier">order</span><span class="special">)</span></code>
                  </p>
                </td>
<td>
                  <p>
                    Return current value
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="keyword">void</span> <span class="identifier">store</span><span class="special">(</span><span class="identifier">T</span>
                    <span class="identifier">value</span><span class="special">,</span>
                    <span class="identifier">memory_order</span> <span class="identifier">order</span><span class="special">)</span></code>
                  </p>
                </td>
<td>
                  <p>
                    Write new value to atomic variable
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">T</span> <span class="identifier">exchange</span><span class="special">(</span><span class="identifier">T</span>
                    <span class="identifier">new_value</span><span class="special">,</span>
                    <span class="identifier">memory_order</span> <span class="identifier">order</span><span class="special">)</span></code>
                  </p>
                </td>
<td>
                  <p>
                    Exchange current value with <code class="computeroutput"><span class="identifier">new_value</span></code>,
                    returning current value
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="keyword">bool</span> <span class="identifier">compare_exchange_weak</span><span class="special">(</span><span class="identifier">T</span>
                    <span class="special">&amp;</span> <span class="identifier">expected</span><span class="special">,</span> <span class="identifier">T</span>
                    <span class="identifier">desired</span><span class="special">,</span>
                    <span class="identifier">memory_order</span> <span class="identifier">order</span><span class="special">)</span></code>
                  </p>
                </td>
<td>
                  <p>
                    Compare current value with <code class="computeroutput"><span class="identifier">expected</span></code>,
                    change it to <code class="computeroutput"><span class="identifier">desired</span></code>
                    if matches. Returns <code class="computeroutput"><span class="keyword">true</span></code>
                    if an exchange has been performed, and always writes the previous
                    value back in <code class="computeroutput"><span class="identifier">expected</span></code>.
                    May fail spuriously, so must generally be retried in a loop.
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="keyword">bool</span> <span class="identifier">compare_exchange_weak</span><span class="special">(</span><span class="identifier">T</span>
                    <span class="special">&amp;</span> <span class="identifier">expected</span><span class="special">,</span> <span class="identifier">T</span>
                    <span class="identifier">desired</span><span class="special">,</span>
                    <span class="identifier">memory_order</span> <span class="identifier">success_order</span><span class="special">,</span> <span class="identifier">memory_order</span>
                    <span class="identifier">failure_order</span><span class="special">)</span></code>
                  </p>
                </td>
<td>
                  <p>
                    Compare current value with <code class="computeroutput"><span class="identifier">expected</span></code>,
                    change it to <code class="computeroutput"><span class="identifier">desired</span></code>
                    if matches. Returns <code class="computeroutput"><span class="keyword">true</span></code>
                    if an exchange has been performed, and always writes the previous
                    value back in <code class="computeroutput"><span class="identifier">expected</span></code>.
                    May fail spuriously, so must generally be retried in a loop.
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="keyword">bool</span> <span class="identifier">compare_exchange_strong</span><span class="special">(</span><span class="identifier">T</span>
                    <span class="special">&amp;</span> <span class="identifier">expected</span><span class="special">,</span> <span class="identifier">T</span>
                    <span class="identifier">desired</span><span class="special">,</span>
                    <span class="identifier">memory_order</span> <span class="identifier">order</span><span class="special">)</span></code>
                  </p>
                </td>
<td>
                  <p>
                    Compare current value with <code class="computeroutput"><span class="identifier">expected</span></code>,
                    change it to <code class="computeroutput"><span class="identifier">desired</span></code>
                    if matches. Returns <code class="computeroutput"><span class="keyword">true</span></code>
                    if an exchange has been performed, and always writes the previous
                    value back in <code class="computeroutput"><span class="identifier">expected</span></code>.
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="keyword">bool</span> <span class="identifier">compare_exchange_strong</span><span class="special">(</span><span class="identifier">T</span>
                    <span class="special">&amp;</span> <span class="identifier">expected</span><span class="special">,</span> <span class="identifier">T</span>
                    <span class="identifier">desired</span><span class="special">,</span>
                    <span class="identifier">memory_order</span> <span class="identifier">success_order</span><span class="special">,</span> <span class="identifier">memory_order</span>
                    <span class="identifier">failure_order</span><span class="special">))</span></code>
                  </p>
                </td>
<td>
                  <p>
                    Compare current value with <code class="computeroutput"><span class="identifier">expected</span></code>,
                    change it to <code class="computeroutput"><span class="identifier">desired</span></code>
                    if matches. Returns <code class="computeroutput"><span class="keyword">true</span></code>
                    if an exchange has been performed, and always writes the previous
                    value back in <code class="computeroutput"><span class="identifier">expected</span></code>.
                  </p>
                </td>
</tr>
</tbody>
</table></div>
<p>
          <code class="computeroutput"><span class="identifier">order</span></code> always has <code class="computeroutput"><span class="identifier">memory_order_seq_cst</span></code> as default parameter.
        </p>
<p>
          The <code class="computeroutput"><span class="identifier">compare_exchange_weak</span></code>/<code class="computeroutput"><span class="identifier">compare_exchange_strong</span></code> variants taking
          four parameters differ from the three parameter variants in that they allow
          a different memory ordering constraint to be specified in case the operation
          fails.
        </p>
<p>
          In addition to these explicit operations, each <code class="literal">atomic&lt;<span class="emphasis"><em>T</em></span>&gt;</code>
          object also supports implicit <code class="literal">store</code> and <code class="literal">load</code>
          through the use of "assignment" and "conversion to <code class="literal">T</code>"
          operators. Avoid using these operators, as they do not allow explicit specification
          of a memory ordering constraint.
        </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="atomic.interface.interface_atomic_object.interface_atomic_integral"></a><a class="link" href="interface.html#atomic.interface.interface_atomic_object.interface_atomic_integral" title="boost::atomic&lt;integral&gt; template class"><code class="literal">boost::atomic&lt;<span class="emphasis"><em>integral</em></span>&gt;</code>
        template class</a>
</h4></div></div></div>
<p>
          In addition to the operations listed in the previous section, <code class="literal">boost::atomic&lt;<span class="emphasis"><em>I</em></span>&gt;</code>
          for integral types <code class="literal"><span class="emphasis"><em>I</em></span></code> supports the
          following operations:
        </p>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                  <p>
                    Syntax
                  </p>
                </th>
<th>
                  <p>
                    Description
                  </p>
                </th>
</tr></thead>
<tbody>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">T</span> <span class="identifier">fetch_add</span><span class="special">(</span><span class="identifier">T</span>
                    <span class="identifier">v</span><span class="special">,</span>
                    <span class="identifier">memory_order</span> <span class="identifier">order</span><span class="special">)</span></code>
                  </p>
                </td>
<td>
                  <p>
                    Add <code class="computeroutput"><span class="identifier">v</span></code> to variable,
                    returning previous value
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">T</span> <span class="identifier">fetch_sub</span><span class="special">(</span><span class="identifier">T</span>
                    <span class="identifier">v</span><span class="special">,</span>
                    <span class="identifier">memory_order</span> <span class="identifier">order</span><span class="special">)</span></code>
                  </p>
                </td>
<td>
                  <p>
                    Subtract <code class="computeroutput"><span class="identifier">v</span></code> from
                    variable, returning previous value
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">T</span> <span class="identifier">fetch_and</span><span class="special">(</span><span class="identifier">T</span>
                    <span class="identifier">v</span><span class="special">,</span>
                    <span class="identifier">memory_order</span> <span class="identifier">order</span><span class="special">)</span></code>
                  </p>
                </td>
<td>
                  <p>
                    Apply bit-wise "and" with <code class="computeroutput"><span class="identifier">v</span></code>
                    to variable, returning previous value
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">T</span> <span class="identifier">fetch_or</span><span class="special">(</span><span class="identifier">T</span>
                    <span class="identifier">v</span><span class="special">,</span>
                    <span class="identifier">memory_order</span> <span class="identifier">order</span><span class="special">)</span></code>
                  </p>
                </td>
<td>
                  <p>
                    Apply bit-wise "or" with <code class="computeroutput"><span class="identifier">v</span></code>
                    to variable, returning previous value
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">T</span> <span class="identifier">fetch_xor</span><span class="special">(</span><span class="identifier">T</span>
                    <span class="identifier">v</span><span class="special">,</span>
                    <span class="identifier">memory_order</span> <span class="identifier">order</span><span class="special">)</span></code>
                  </p>
                </td>
<td>
                  <p>
                    Apply bit-wise "xor" with <code class="computeroutput"><span class="identifier">v</span></code>
                    to variable, returning previous value
                  </p>
                </td>
</tr>
</tbody>
</table></div>
<p>
          <code class="computeroutput"><span class="identifier">order</span></code> always has <code class="computeroutput"><span class="identifier">memory_order_seq_cst</span></code> as default parameter.
        </p>
<p>
          In addition to these explicit operations, each <code class="literal">boost::atomic&lt;<span class="emphasis"><em>I</em></span>&gt;</code>
          object also supports implicit pre-/post- increment/decrement, as well as
          the operators <code class="computeroutput"><span class="special">+=</span></code>, <code class="computeroutput"><span class="special">-=</span></code>, <code class="computeroutput"><span class="special">&amp;=</span></code>,
          <code class="computeroutput"><span class="special">|=</span></code> and <code class="computeroutput"><span class="special">^=</span></code>.
          Avoid using these operators, as they do not allow explicit specification
          of a memory ordering constraint.
        </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="atomic.interface.interface_atomic_object.interface_atomic_pointer"></a><a class="link" href="interface.html#atomic.interface.interface_atomic_object.interface_atomic_pointer" title="boost::atomic&lt;pointer&gt; template class"><code class="literal">boost::atomic&lt;<span class="emphasis"><em>pointer</em></span>&gt;</code>
        template class</a>
</h4></div></div></div>
<p>
          In addition to the operations applicable to all atomic object, <code class="literal">boost::atomic&lt;<span class="emphasis"><em>P</em></span>&gt;</code>
          for pointer types <code class="literal"><span class="emphasis"><em>P</em></span></code> (other than
          <code class="literal">void</code> pointers) support the following operations:
        </p>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                  <p>
                    Syntax
                  </p>
                </th>
<th>
                  <p>
                    Description
                  </p>
                </th>
</tr></thead>
<tbody>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">T</span> <span class="identifier">fetch_add</span><span class="special">(</span><span class="identifier">ptrdiff_t</span>
                    <span class="identifier">v</span><span class="special">,</span>
                    <span class="identifier">memory_order</span> <span class="identifier">order</span><span class="special">)</span></code>
                  </p>
                </td>
<td>
                  <p>
                    Add <code class="computeroutput"><span class="identifier">v</span></code> to variable,
                    returning previous value
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">T</span> <span class="identifier">fetch_sub</span><span class="special">(</span><span class="identifier">ptrdiff_t</span>
                    <span class="identifier">v</span><span class="special">,</span>
                    <span class="identifier">memory_order</span> <span class="identifier">order</span><span class="special">)</span></code>
                  </p>
                </td>
<td>
                  <p>
                    Subtract <code class="computeroutput"><span class="identifier">v</span></code> from
                    variable, returning previous value
                  </p>
                </td>
</tr>
</tbody>
</table></div>
<p>
          <code class="computeroutput"><span class="identifier">order</span></code> always has <code class="computeroutput"><span class="identifier">memory_order_seq_cst</span></code> as default parameter.
        </p>
<p>
          In addition to these explicit operations, each <code class="literal">boost::atomic&lt;<span class="emphasis"><em>P</em></span>&gt;</code>
          object also supports implicit pre-/post- increment/decrement, as well as
          the operators <code class="computeroutput"><span class="special">+=</span></code>, <code class="computeroutput"><span class="special">-=</span></code>. Avoid using these operators, as they
          do not allow explicit specification of a memory ordering constraint.
        </p>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="atomic.interface.interface_fences"></a><a class="link" href="interface.html#atomic.interface.interface_fences" title="Fences">Fences</a>
</h3></div></div></div>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">atomic</span><span class="special">/</span><span class="identifier">fences</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
</pre>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                <p>
                  Syntax
                </p>
              </th>
<th>
                <p>
                  Description
                </p>
              </th>
</tr></thead>
<tbody>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="keyword">void</span> <span class="identifier">atomic_thread_fence</span><span class="special">(</span><span class="identifier">memory_order</span>
                  <span class="identifier">order</span><span class="special">)</span></code>
                </p>
              </td>
<td>
                <p>
                  Issue fence for coordination with other threads.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="keyword">void</span> <span class="identifier">atomic_signal_fence</span><span class="special">(</span><span class="identifier">memory_order</span>
                  <span class="identifier">order</span><span class="special">)</span></code>
                </p>
              </td>
<td>
                <p>
                  Issue fence for coordination with signal handler (only in same
                  thread).
                </p>
              </td>
</tr>
</tbody>
</table></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="atomic.interface.feature_macros"></a><a class="link" href="interface.html#atomic.interface.feature_macros" title="Feature testing macros">Feature testing macros</a>
</h3></div></div></div>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">atomic</span><span class="special">/</span><span class="identifier">capabilities</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
</pre>
<p>
        <span class="bold"><strong>Boost.Atomic</strong></span> defines a number of macros
        to allow compile-time detection whether an atomic data type is implemented
        using "true" atomic operations, or whether an internal "lock"
        is used to provide atomicity. The following macros will be defined to <code class="computeroutput"><span class="number">0</span></code> if operations on the data type always require
        a lock, to <code class="computeroutput"><span class="number">1</span></code> if operations on
        the data type may sometimes require a lock, and to <code class="computeroutput"><span class="number">2</span></code>
        if they are always lock-free:
      </p>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                <p>
                  Macro
                </p>
              </th>
<th>
                <p>
                  Description
                </p>
              </th>
</tr></thead>
<tbody>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_FLAG_LOCK_FREE</span></code>
                </p>
              </td>
<td>
                <p>
                  Indicate whether <code class="computeroutput"><span class="identifier">atomic_flag</span></code>
                  is lock-free
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_BOOL_LOCK_FREE</span></code>
                </p>
              </td>
<td>
                <p>
                  Indicate whether <code class="computeroutput"><span class="identifier">atomic</span><span class="special">&lt;</span><span class="keyword">bool</span><span class="special">&gt;</span></code> is lock-free
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_CHAR_LOCK_FREE</span></code>
                </p>
              </td>
<td>
                <p>
                  Indicate whether <code class="computeroutput"><span class="identifier">atomic</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">&gt;</span></code> (including signed/unsigned
                  variants) is lock-free
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_CHAR16_T_LOCK_FREE</span></code>
                </p>
              </td>
<td>
                <p>
                  Indicate whether <code class="computeroutput"><span class="identifier">atomic</span><span class="special">&lt;</span><span class="keyword">char16_t</span><span class="special">&gt;</span></code> (including signed/unsigned
                  variants) is lock-free
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_CHAR32_T_LOCK_FREE</span></code>
                </p>
              </td>
<td>
                <p>
                  Indicate whether <code class="computeroutput"><span class="identifier">atomic</span><span class="special">&lt;</span><span class="keyword">char32_t</span><span class="special">&gt;</span></code> (including signed/unsigned
                  variants) is lock-free
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_WCHAR_T_LOCK_FREE</span></code>
                </p>
              </td>
<td>
                <p>
                  Indicate whether <code class="computeroutput"><span class="identifier">atomic</span><span class="special">&lt;</span><span class="keyword">wchar_t</span><span class="special">&gt;</span></code> (including signed/unsigned
                  variants) is lock-free
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_SHORT_LOCK_FREE</span></code>
                </p>
              </td>
<td>
                <p>
                  Indicate whether <code class="computeroutput"><span class="identifier">atomic</span><span class="special">&lt;</span><span class="keyword">short</span><span class="special">&gt;</span></code> (including signed/unsigned
                  variants) is lock-free
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_INT_LOCK_FREE</span></code>
                </p>
              </td>
<td>
                <p>
                  Indicate whether <code class="computeroutput"><span class="identifier">atomic</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span></code> (including signed/unsigned
                  variants) is lock-free
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_LONG_LOCK_FREE</span></code>
                </p>
              </td>
<td>
                <p>
                  Indicate whether <code class="computeroutput"><span class="identifier">atomic</span><span class="special">&lt;</span><span class="keyword">long</span><span class="special">&gt;</span></code> (including signed/unsigned
                  variants) is lock-free
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_LLONG_LOCK_FREE</span></code>
                </p>
              </td>
<td>
                <p>
                  Indicate whether <code class="computeroutput"><span class="identifier">atomic</span><span class="special">&lt;</span><span class="keyword">long</span>
                  <span class="keyword">long</span><span class="special">&gt;</span></code>
                  (including signed/unsigned variants) is lock-free
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_ADDRESS_LOCK_FREE</span></code>
                  or <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_POINTER_LOCK_FREE</span></code>
                </p>
              </td>
<td>
                <p>
                  Indicate whether <code class="computeroutput"><span class="identifier">atomic</span><span class="special">&lt;</span><span class="identifier">T</span>
                  <span class="special">*&gt;</span></code> is lock-free
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_THREAD_FENCE</span></code>
                </p>
              </td>
<td>
                <p>
                  Indicate whether <code class="computeroutput"><span class="identifier">atomic_thread_fence</span></code>
                  function is lock-free
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_SIGNAL_FENCE</span></code>
                </p>
              </td>
<td>
                <p>
                  Indicate whether <code class="computeroutput"><span class="identifier">atomic_signal_fence</span></code>
                  function is lock-free
                </p>
              </td>
</tr>
</tbody>
</table></div>
<p>
        In addition to these standard macros, <span class="bold"><strong>Boost.Atomic</strong></span>
        also defines a number of extension macros, which can also be useful. Like
        the standard ones, these macros are defined to values <code class="computeroutput"><span class="number">0</span></code>,
        <code class="computeroutput"><span class="number">1</span></code> and <code class="computeroutput"><span class="number">2</span></code>
        to indicate whether the corresponding operations are lock-free or not.
      </p>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                <p>
                  Macro
                </p>
              </th>
<th>
                <p>
                  Description
                </p>
              </th>
</tr></thead>
<tbody>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_INT8_LOCK_FREE</span></code>
                </p>
              </td>
<td>
                <p>
                  Indicate whether <code class="computeroutput"><span class="identifier">atomic</span><span class="special">&lt;</span><span class="identifier">int8_type</span><span class="special">&gt;</span></code> is lock-free.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_INT16_LOCK_FREE</span></code>
                </p>
              </td>
<td>
                <p>
                  Indicate whether <code class="computeroutput"><span class="identifier">atomic</span><span class="special">&lt;</span><span class="identifier">int16_type</span><span class="special">&gt;</span></code> is lock-free.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_INT32_LOCK_FREE</span></code>
                </p>
              </td>
<td>
                <p>
                  Indicate whether <code class="computeroutput"><span class="identifier">atomic</span><span class="special">&lt;</span><span class="identifier">int32_type</span><span class="special">&gt;</span></code> is lock-free.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_INT64_LOCK_FREE</span></code>
                </p>
              </td>
<td>
                <p>
                  Indicate whether <code class="computeroutput"><span class="identifier">atomic</span><span class="special">&lt;</span><span class="identifier">int64_type</span><span class="special">&gt;</span></code> is lock-free.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_INT128_LOCK_FREE</span></code>
                </p>
              </td>
<td>
                <p>
                  Indicate whether <code class="computeroutput"><span class="identifier">atomic</span><span class="special">&lt;</span><span class="identifier">int128_type</span><span class="special">&gt;</span></code> is lock-free.
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_NO_ATOMIC_FLAG_INIT</span></code>
                </p>
              </td>
<td>
                <p>
                  Defined after including <code class="computeroutput"><span class="identifier">atomic_flag</span><span class="special">.</span><span class="identifier">hpp</span></code>,
                  if the implementation does not support the <code class="computeroutput"><span class="identifier">BOOST_ATOMIC_FLAG_INIT</span></code>
                  macro for static initialization of <code class="computeroutput"><span class="identifier">atomic_flag</span></code>.
                  This macro is typically defined for pre-C++11 compilers.
                </p>
              </td>
</tr>
</tbody>
</table></div>
<p>
        In the table above, <code class="computeroutput"><span class="identifier">intN_type</span></code>
        is a type that fits storage of contiguous <code class="computeroutput"><span class="identifier">N</span></code>
        bits, suitably aligned for atomic operations.
      </p>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2011 Helge Bahmann<br>Copyright &#169; 2012 Tim Blechmann<br>Copyright &#169; 2013 Andrey Semashev<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="thread_coordination.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../atomic.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="usage_examples.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>