OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [gcc-4.5.1/] [libstdc++-v3/] [doc/] [xml/] [manual/] [using.xml] - Blame information for rev 826

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 424 jeremybenn
2
3
 "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd"
4
[ ]>
5
 
6
7
  
8
 
9
Using
10
 
11
 
12
  
13
    Command Options
14
    
15
      The set of features available in the GNU C++ library is shaped
16
      by
17
      several GCC
18
      Command Options. Options that impact libstdc++ are
19
      enumerated and detailed in the table below.
20
    
21
 
22
    
23
      By default, g++ is equivalent to  g++ -std=gnu++98. The standard library also defaults to this dialect.
24
    
25
 
26
 
27
C++ Command Options
28
29
30
31
 
32
  
33
    
34
      Option Flags
35
      Description
36
    
37
  
38
 
39
  
40
    
41
      -std=c++98
42
      Use the 1998 ISO C++ standard plus amendments.
43
    
44
 
45
    
46
      -std=gnu++98
47
      As directly above, with GNU extensions.
48
    
49
 
50
    
51
      -std=c++0x
52
      Use the working draft of the upcoming ISO C++0x standard.
53
    
54
 
55
    
56
      -std=gnu++0x
57
      As directly above, with GNU extensions.
58
    
59
 
60
    
61
      -fexceptions
62
      See exception-free dialect
63
    
64
 
65
    
66
      -frtti
67
      As above, but RTTI-free dialect.
68
    
69
 
70
    
71
      -pthread or -pthreads
72
      For ISO C++0x <thread>, <future>,
73
      <mutex>, or <condition_variable>.
74
    
75
 
76
    
77
      -fopenmp
78
      For parallel mode.
79
    
80
  
81
 
82
83
84
 
85
  
86
 
87
  
88
    
89
    Headers
90
 
91
    
92
      Header Files
93
 
94
   
95
     The C++ standard specifies the entire set of header files that
96
     must be available to all hosted implementations.  Actually, the
97
     word "files" is a misnomer, since the contents of the
98
     headers don't necessarily have to be in any kind of external
99
     file.  The only rule is that when one #include's a
100
     header, the contents of that header become available, no matter
101
     how.
102
   
103
 
104
   
105
   That said, in practice files are used.
106
   
107
 
108
   
109
     There are two main types of include files: header files related
110
     to a specific version of the ISO C++ standard (called Standard
111
     Headers), and all others (TR1, C++ ABI, and Extensions).
112
   
113
 
114
   
115
     Two dialects of standard headers are supported, corresponding to
116
     the 1998 standard as updated for 2003, and the draft of the
117
     upcoming 200x standard.
118
   
119
 
120
   
121
     C++98/03 include files. These are available in the default compilation mode, i.e. -std=c++98 or -std=gnu++98.
122
   
123
 
124
125
C++ 1998 Library Headers
126
127
128
129
130
131
132
133
134
algorithm
135
bitset
136
complex
137
deque
138
exception
139
140
141
fstream
142
functional
143
iomanip
144
ios
145
iosfwd
146
147
148
iostream
149
istream
150
iterator
151
limits
152
list
153
154
155
locale
156
map
157
memory
158
new
159
numeric
160
161
162
ostream
163
queue
164
set
165
sstream
166
stack
167
168
169
stdexcept
170
streambuf
171
string
172
utility
173
typeinfo
174
175
176
valarray
177
vector
178
179
180
181
182
 
183
184
185
C++ 1998 Library Headers for C Library Facilities
186
187
188
189
190
191
192
193
194
cassert
195
cerrno
196
cctype
197
cfloat
198
ciso646
199
200
201
climits
202
clocale
203
cmath
204
csetjmp
205
csignal
206
207
208
cstdarg
209
cstddef
210
cstdio
211
cstdlib
212
cstring
213
214
215
ctime
216
cwchar
217
cwctype
218
219
220
221
222
 
223
224
C++0x include files. These are only available in C++0x compilation
225
mode, i.e. -std=c++0x or -std=gnu++0x.
226
227
 
228
229
230
C++ 200x Library Headers
231
232
233
234
235
236
237
238
 
239
240
algorithm
241
array
242
bitset
243
chrono
244
complex
245
246
247
condition_variable
248
deque
249
exception
250
forward_list
251
fstream
252
253
254
functional
255
future
256
initalizer_list
257
iomanip
258
ios
259
260
261
iosfwd
262
iostream
263
istream
264
iterator
265
limits
266
267
268
list
269
locale
270
map
271
memory
272
mutex
273
274
275
new
276
numeric
277
ostream
278
queue
279
random
280
281
282
ratio
283
regex
284
set
285
sstream
286
stack
287
288
289
stdexcept
290
streambuf
291
string
292
system_error
293
thread
294
295
296
tuple
297
type_traits
298
typeinfo
299
unordered_map
300
unordered_set
301
302
303
utility
304
valarray
305
vector
306
307
 
308
309
310
311
 
312
313
 
314
315
C++ 200x Library Headers for C Library Facilities
316
317
318
319
320
321
322
323
324
cassert
325
ccomplex
326
cctype
327
cerrno
328
cfenv
329
330
331
cfloat
332
cinttypes
333
ciso646
334
climits
335
clocale
336
337
338
cmath
339
csetjmp
340
csignal
341
cstdarg
342
cstdbool
343
344
345
cstddef
346
cstdint
347
cstdlib
348
cstdio
349
cstring
350
351
352
ctgmath
353
ctime
354
cuchar
355
cwchar
356
cwctype
357
358
359
stdatomic.h
360
361
 
362
363
364
365
 
366
 
367
368
  In addition, TR1 includes as:
369
370
 
371
372
C++ TR 1 Library Headers
373
374
375
376
377
378
379
380
 
381
382
tr1/array
383
tr1/complex
384
tr1/memory
385
tr1/functional
386
tr1/random
387
388
389
tr1/regex
390
tr1/tuple
391
tr1/type_traits
392
tr1/unordered_map
393
tr1/unordered_set
394
395
396
tr1/utility
397
398
 
399
400
401
402
 
403
404
 
405
 
406
407
C++ TR 1 Library Headers for C Library Facilities
408
409
410
411
412
413
414
415
 
416
417
tr1/ccomplex
418
tr1/cfenv
419
tr1/cfloat
420
tr1/cmath
421
tr1/cinttypes
422
423
424
tr1/climits
425
tr1/cstdarg
426
tr1/cstdbool
427
tr1/cstdint
428
tr1/cstdio
429
430
431
tr1/cstdlib
432
tr1/ctgmath
433
tr1/ctime
434
tr1/cwchar
435
tr1/cwctype
436
437
 
438
439
440
441
 
442
 
443
Decimal floating-point arithmetic is available if the C++
444
compiler supports scalar decimal floating-point types defined via
445
__attribute__((mode(SD|DD|LD))).
446
447
 
448
449
C++ TR 24733 Decimal Floating-Point Header
450
451
452
453
454
decimal/decimal
455
456
457
458
459
 
460
461
  Also included are files for the C++ ABI interface:
462
463
 
464
465
C++ ABI Headers
466
467
468
469
470
cxxabi.hcxxabi_forced.h
471
472
473
474
 
475
476
  And a large variety of extensions.
477
478
 
479
480
Extension Headers
481
482
483
484
485
486
487
488
 
489
490
ext/algorithm
491
ext/atomicity.h
492
ext/array_allocator.h
493
ext/bitmap_allocator.h
494
ext/cast.h
495
496
497
ext/codecvt_specializations.h
498
ext/concurrence.h
499
ext/debug_allocator.h
500
ext/enc_filebuf.h
501
ext/extptr_allocator.h
502
503
504
ext/functional
505
ext/iterator
506
ext/malloc_allocator.h
507
ext/memory
508
ext/mt_allocator.h
509
510
511
ext/new_allocator.h
512
ext/numeric
513
ext/numeric_traits.h
514
ext/pb_ds/assoc_container.h
515
ext/pb_ds/priority_queue.h
516
517
518
ext/pod_char_traits.h
519
ext/pool_allocator.h
520
ext/rb_tree
521
ext/rope
522
ext/slist
523
524
525
ext/stdio_filebuf.h
526
ext/stdio_sync_filebuf.h
527
ext/throw_allocator.h
528
ext/typelist.h
529
ext/type_traits.h
530
531
532
ext/vstring.h
533
534
 
535
536
537
538
 
539
540
 
541
542
Extension Debug Headers
543
544
545
546
547
548
549
550
 
551
552
debug/bitset
553
debug/deque
554
debug/list
555
debug/map
556
debug/set
557
558
 
559
560
debug/string
561
debug/unordered_map
562
debug/unordered_set
563
debug/vector
564
565
 
566
567
568
569
 
570
571
 
572
573
Extension Profile Headers
574
575
576
577
578
579
580
 
581
582
profile/bitset
583
profile/deque
584
profile/list
585
profile/map
586
587
 
588
589
profile/set
590
profile/unordered_map
591
profile/unordered_set
592
profile/vector
593
594
 
595
596
597
598
 
599
600
 
601
602
Extension Parallel Headers
603
604
605
606
607
608
parallel/algorithm
609
parallel/numeric
610
611
612
613
614
 
615
    
616
 
617
    
618
      Mixing Headers
619
 
620
 A few simple rules.
621
622
 
623
First, mixing different dialects of the standard headers is not
624
possible. It's an all-or-nothing affair. Thus, code like
625
626
 
627
628
#include <array>
629
#include <functional>
630
631
 
632
Implies C++0x mode. To use the entities in <array>, the C++0x
633
compilation mode must be used, which implies the C++0x functionality
634
(and deprecations) in <functional> will be present.
635
636
 
637
Second, the other headers can be included with either dialect of
638
the standard headers, although features and types specific to C++0x
639
are still only enabled when in C++0x compilation mode. So, to use
640
rvalue references with __gnu_cxx::vstring, or to use the
641
debug-mode versions of std::unordered_map, one must use
642
the std=gnu++0x compiler flag. (Or std=c++0x, of course.)
643
644
 
645
A special case of the second rule is the mixing of TR1 and C++0x
646
facilities. It is possible (although not especially prudent) to
647
include both the TR1 version and the C++0x version of header in the
648
same translation unit:
649
650
 
651
652
#include <tr1/type_traits>
653
#include <type_traits>
654
655
 
656
 Several parts of C++0x diverge quite substantially from TR1 predecessors.
657
658
    
659
 
660
    
661
      The C Headers and <code>namespace std</code>
662
 
663
664
        The standard specifies that if one includes the C-style header
665
        (<math.h> in this case), the symbols will be available
666
        in the global namespace and perhaps in
667
        namespace std:: (but this is no longer a firm
668
        requirement.) On the other hand, including the C++-style
669
        header (<cmath>) guarantees that the entities will be
670
        found in namespace std and perhaps in the global namespace.
671
      
672
 
673
674
Usage of C++-style headers is recommended, as then
675
C-linkage names can be disambiguated by explicit qualification, such
676
as by std::abort. In addition, the C++-style headers can
677
use function overloading to provide a simpler interface to certain
678
families of C-functions. For instance in <cmath>, the
679
function std::sin has overloads for all the builtin
680
floating-point types. This means that std::sin can be
681
used uniformly, instead of a combination
682
of std::sinf, std::sin,
683
and std::sinl.
684
685
    
686
 
687
    
688
      Precompiled Headers
689
 
690
 
691
There are three base header files that are provided. They can be
692
used to precompile the standard headers and extensions into binary
693
files that may the be used to speed compiles that use these headers.
694
695
 
696
 
697
698
699
  stdc++.h
700
Includes all standard headers. Actual content varies depending on
701
language dialect.
702
703
704
 
705
706
  stdtr1c++.h
707
Includes all of <stdc++.h>, and adds all the TR1 headers.
708
709
710
 
711
extc++.h
712
Includes all of <stdtr1c++.h>, and adds all the Extension headers.
713
714
715
 
716
How to construct a .gch file from one of these base header files.
717
 
718
First, find the include directory for the compiler. One way to do
719
this is:
720
 
721
722
g++ -v hello.cc
723
 
724
#include <...> search starts here:
725
 /mnt/share/bld/H-x86-gcc.20071201/include/c++/4.3.0
726
...
727
End of search list.
728
729
 
730
 
731
Then, create a precompiled header file with the same flags that
732
will be used to compile other projects.
733
 
734
735
g++ -Winvalid-pch -x c++-header -g -O2 -o ./stdc++.h.gch /mnt/share/bld/H-x86-gcc.20071201/include/c++/4.3.0/x86_64-unknown-linux-gnu/bits/stdc++.h
736
737
 
738
The resulting file will be quite large: the current size is around
739
thirty megabytes. 
740
 
741
How to use the resulting file.
742
 
743
744
g++ -I. -include stdc++.h  -H -g -O2 hello.cc
745
746
 
747
Verification that the PCH file is being used is easy:
748
 
749
750
g++ -Winvalid-pch -I. -include stdc++.h -H -g -O2 hello.cc -o test.exe
751
! ./stdc++.h.gch
752
. /mnt/share/bld/H-x86-gcc.20071201/include/c++/4.3.0/iostream
753
. /mnt/share/bld/H-x86-gcc.20071201include/c++/4.3.0/string
754
755
 
756
The exclamation point to the left of the stdc++.h.gch listing means that the generated PCH file was used, and thus the 
757
758
 
759
 Detailed information about creating precompiled header files can be found in the GCC documentation.
760
761
 
762
    
763
  
764
 
765
 
766
  
767
    
768
    Macros
769
 
770
   
771
     All library macros begin with _GLIBCXX_.
772
   
773
 
774
   
775
     Furthermore, all pre-processor macros, switches, and
776
      configuration options are gathered in the
777
      file c++config.h, which
778
      is generated during the libstdc++ configuration and build
779
      process. This file is then included when needed by files part of
780
      the public libstdc++ API, like <ios>. Most of these macros
781
      should not be used by consumers of libstdc++, and are reserved
782
      for internal implementation use. These macros cannot
783
      be redefined.
784
   
785
 
786
   
787
     A select handful of macros control libstdc++ extensions and extra
788
      features, or provide versioning information for the API.  Only
789
      those macros listed below are offered for consideration by the
790
      general public.
791
   
792
 
793
   Below is the macro which users may check for library version
794
      information. 
795
 
796
    
797
    
798
      __GLIBCXX__
799
      
800
        The current version of
801
    libstdc++ in compressed ISO date format, form of an unsigned
802
    long. For details on the value of this particular macro for a
803
    particular release, please consult this 
804
    document.
805
    
806
    
807
    
808
    
809
 
810
   Below are the macros which users may change with #define/#undef or
811
      with -D/-U compiler flags.  The default state of the symbol is
812
      listed.
813
 
814
   Configurable (or Not configurable) means
815
      that the symbol is initially chosen (or not) based on
816
      --enable/--disable options at library build and configure time
817
      (documented here), with the
818
      various --enable/--disable choices being translated to
819
      #define/#undef).
820
   
821
 
822
    ABI means that changing from the default value may
823
  mean changing the ABI of compiled code. In other words, these
824
  choices control code which has already been compiled (i.e., in a
825
  binary such as libstdc++.a/.so).  If you explicitly #define or
826
  #undef these macros, the headers may see different code
827
  paths, but the libraries which you link against will not.
828
  Experimenting with different values with the expectation of
829
  consistent linkage requires changing the config headers before
830
  building/installing the library.
831
   
832
 
833
    
834
    _GLIBCXX_DEPRECATED
835
    
836
      
837
        Defined by default. Not configurable. ABI-changing. Turning this off
838
        removes older ARM-style iostreams code, and other anachronisms
839
        from the API.  This macro is dependent on the version of the
840
        standard being tracked, and as a result may give different results for
841
        -std=c++98 and -std=c++0x. This may
842
        be useful in updating old C++ code which no longer meet the
843
        requirements of the language, or for checking current code
844
        against new language standards.
845
    
846
    
847
 
848
    _GLIBCXX_FORCE_NEW
849
    
850
      
851
        Undefined by default. When defined, memory allocation and
852
        allocators controlled by libstdc++ call operator new/delete
853
        without caching and pooling. Configurable via
854
        --enable-libstdcxx-allocator. ABI-changing.
855
      
856
    
857
 
858
 
859
    _GLIBCXX_CONCEPT_CHECKS
860
    
861
      
862
        Undefined by default.  Configurable via
863
        --enable-concept-checks.  When defined, performs
864
        compile-time checking on certain template instantiations to
865
        detect violations of the requirements of the standard.  This
866
        is described in more detail 
867
        linkend="manual.ext.compile_checks">here.
868
      
869
    
870
 
871
    _GLIBCXX_DEBUG
872
    
873
      
874
        Undefined by default. When defined, compiles user code using
875
    the debug mode.
876
      
877
    
878
    _GLIBCXX_DEBUG_PEDANTIC
879
    
880
      
881
        Undefined by default. When defined while compiling with
882
    the debug mode, makes
883
    the debug mode extremely picky by making the use of libstdc++
884
    extensions and libstdc++-specific behavior into errors.
885
      
886
    
887
    _GLIBCXX_PARALLEL
888
    
889
      Undefined by default. When defined, compiles user code
890
    using the parallel
891
    mode.
892
      
893
    
894
 
895
    _GLIBCXX_PROFILE
896
    
897
      Undefined by default. When defined, compiles user code
898
    using the profile
899
    mode.
900
      
901
    
902
    
903
 
904
  
905
 
906
  
907
    
908
    Namespaces
909
 
910
    
911
      Available Namespaces
912
 
913
 
914
 
915
 There are three main namespaces.
916
917
 
918
919
  std
920
The ISO C++ standards specify that "all library entities are defined
921
within namespace std." This includes namespaces nested
922
within namespace std, such as namespace
923
std::tr1.
924
925
926
abi
927
Specified by the C++ ABI. This ABI specifies a number of type and
928
function APIs supplemental to those required by the ISO C++ Standard,
929
but necessary for interoperability.
930
931
932
 
933
__gnu_
934
Indicating one of several GNU extensions. Choices
935
include __gnu_cxx, __gnu_debug, __gnu_parallel,
936
and __gnu_pbds.
937
938
939
 
940
 A complete list of implementation namespaces (including namespace contents) is available in the generated source documentation.
941
942
 
943
 
944
    
945
 
946
    
947
      namespace std
948
 
949
 
950
951
      One standard requirement is that the library components are defined
952
      in namespace std::. Thus, in order to use these types or
953
      functions, one must do one of two things:
954
955
 
956
957
  put a kind of using-declaration in your source
958
(either using namespace std; or i.e. using
959
std::string;) This approach works well for individual source files, but
960
should not be used in a global context, like header files.
961
           use a fully
962
qualified name for each library symbol
963
(i.e. std::string, std::cout) Always can be
964
used, and usually enhanced, by strategic use of typedefs. (In the
965
cases where the qualified verbiage becomes unwieldy.)
966
          
967
        
968
969
 
970
    
971
 
972
    
973
      Using Namespace Composition
974
 
975
976
Best practice in programming suggests sequestering new data or
977
functionality in a sanely-named, unique namespace whenever
978
possible. This is considered an advantage over dumping everything in
979
the global namespace, as then name look-up can be explicitly enabled or
980
disabled as above, symbols are consistently mangled without repetitive
981
naming prefixes or macros, etc.
982
983
 
984
For instance, consider a project that defines most of its classes in namespace gtk. It is possible to
985
        adapt namespace gtk to namespace std by using a C++-feature called
986
        namespace composition. This is what happens if
987
        a using-declaration is put into a
988
        namespace-definition: the imported symbol(s) gets imported into the
989
        currently active namespace(s). For example:
990
991
992
namespace gtk
993
{
994
  using std::string;
995
  using std::tr1::array;
996
 
997
  class Window { ... };
998
}
999
1000
1001
        In this example, std::string gets imported into
1002
        namespace gtk.  The result is that use of
1003
        std::string inside namespace gtk can just use string, without the explicit qualification.
1004
        As an added bonus,
1005
        std::string does not get imported into
1006
        the global namespace.  Additionally, a more elaborate arrangement can be made for backwards compatibility and portability, whereby the
1007
        using-declarations can wrapped in macros that
1008
        are set based on autoconf-tests to either "" or i.e. using
1009
          std::string; (depending on whether the system has
1010
        libstdc++ in std:: or not).  (ideas from
1011
        Llewelly and Karl Nelson)
1012
1013
 
1014
 
1015
    
1016
  
1017
 
1018
  
1019
    
1020
    Linking
1021
 
1022
    
1023
      Almost Nothing
1024
      
1025
        Or as close as it gets: freestanding. This is a minimal
1026
        configuration, with only partial support for the standard
1027
        library. Assume only the following header files can be used:
1028
      
1029
 
1030
      
1031
        
1032
          
1033
            cstdarg
1034
          
1035
        
1036
 
1037
        
1038
          
1039
          cstddef
1040
          
1041
        
1042
 
1043
        
1044
          
1045
          cstdlib
1046
          
1047
        
1048
 
1049
        
1050
          
1051
          exception
1052
          
1053
        
1054
 
1055
        
1056
          
1057
          limits
1058
          
1059
        
1060
 
1061
        
1062
          
1063
          new
1064
          
1065
        
1066
 
1067
        
1068
          
1069
          exception
1070
          
1071
        
1072
 
1073
        
1074
          
1075
          typeinfo
1076
          
1077
        
1078
      
1079
 
1080
      
1081
        In addition, throw in
1082
      
1083
 
1084
      
1085
        
1086
          
1087
          cxxabi.h.
1088
          
1089
        
1090
      
1091
 
1092
      
1093
        In the
1094
        C++0x dialect add
1095
      
1096
 
1097
      
1098
        
1099
          
1100
          initializer_list
1101
          
1102
        
1103
        
1104
          
1105
          type_traits
1106
          
1107
        
1108
      
1109
 
1110
       There exists a library that offers runtime support for
1111
        just these headers, and it is called
1112
        libsupc++.a. To use it, compile with gcc instead of g++, like so:
1113
      
1114
 
1115
      
1116
        gcc foo.cc -lsupc++
1117
      
1118
 
1119
      
1120
        No attempt is made to verify that only the minimal subset
1121
        identified above is actually used at compile time. Violations
1122
        are diagnosed as undefined symbols at link time.
1123
      
1124
    
1125
 
1126
    
1127
      Finding Dynamic or Shared Libraries
1128
 
1129
    
1130
      If the only library built is the static library
1131
      (libstdc++.a), or if
1132
      specifying static linking, this section is can be skipped.  But
1133
      if building or using a shared library
1134
      (libstdc++.so), then
1135
      additional location information will need to be provided.
1136
    
1137
    
1138
      But how?
1139
    
1140
    
1141
A quick read of the relevant part of the GCC
1142
      manual, Compiling
1143
      C++ Programs, specifies linking against a C++
1144
      library. More details from the
1145
      GCC FAQ,
1146
      which states GCC does not, by default, specify a
1147
      location so that the dynamic linker can find dynamic libraries at
1148
      runtime.
1149
    
1150
    
1151
      Users will have to provide this information.
1152
    
1153
    
1154
      Methods vary for different platforms and different styles, and
1155
      are printed to the screen during installation. To summarize:
1156
    
1157
    
1158
      
1159
        
1160
          At runtime set LD_LIBRARY_PATH in your
1161
          environment correctly, so that the shared library for
1162
          libstdc++ can be found and loaded.  Be certain that you
1163
          understand all of the other implications and behavior
1164
          of LD_LIBRARY_PATH first.
1165
        
1166
 
1167
      
1168
      
1169
        
1170
          Compile the path to find the library at runtime into the
1171
          program.  This can be done by passing certain options to
1172
          g++, which will in turn pass them on to
1173
          the linker.  The exact format of the options is dependent on
1174
          which linker you use:
1175
        
1176
        
1177
          
1178
            
1179
              GNU ld (default on Linux):-Wl,--rpath,destdir/lib
1180
            
1181
          
1182
          
1183
            
1184
              IRIX ld:
1185
              -Wl,-rpath,destdir/lib
1186
            
1187
          
1188
          
1189
          
1190
          Solaris ld:-Wl,-Rdestdir/lib
1191
          
1192
          
1193
        
1194
      
1195
    
1196
    
1197
      Use the ldd utility on the linked executable
1198
      to show
1199
      which libstdc++.so
1200
      library the system will get at runtime.
1201
    
1202
    
1203
      A libstdc++.la file is
1204
      also installed, for use with Libtool.  If you use Libtool to
1205
      create your executables, these details are taken care of for
1206
      you.
1207
    
1208
    
1209
  
1210
 
1211
 
1212
  
1213
    
1214
    Concurrency
1215
 
1216
   This section discusses issues surrounding the proper compilation
1217
      of multithreaded applications which use the Standard C++
1218
      library.  This information is GCC-specific since the C++
1219
      standard does not address matters of multithreaded applications.
1220
   
1221
 
1222
    
1223
      Prerequisites
1224
 
1225
   All normal disclaimers aside, multithreaded C++ application are
1226
      only supported when libstdc++ and all user code was built with
1227
      compilers which report (via  gcc/g++ -v ) the same thread
1228
      model and that model is not single.  As long as your
1229
      final application is actually single-threaded, then it should be
1230
      safe to mix user code built with a thread model of
1231
      single with a libstdc++ and other C++ libraries built
1232
      with another thread model useful on the platform.  Other mixes
1233
      may or may not work but are not considered supported.  (Thus, if
1234
      you distribute a shared C++ library in binary form only, it may
1235
      be best to compile it with a GCC configured with
1236
      --enable-threads for maximal interchangeability and usefulness
1237
      with a user population that may have built GCC with either
1238
      --enable-threads or --disable-threads.)
1239
   
1240
   When you link a multithreaded application, you will probably
1241
      need to add a library or flag to g++.  This is a very
1242
      non-standardized area of GCC across ports.  Some ports support a
1243
      special flag (the spelling isn't even standardized yet) to add
1244
      all required macros to a compilation (if any such flags are
1245
      required then you must provide the flag for all compilations not
1246
      just linking) and link-library additions and/or replacements at
1247
      link time.  The documentation is weak.  Here is a quick summary
1248
      to display how ad hoc this is: On Solaris, both -pthreads and
1249
      -threads (with subtly different meanings) are honored.  On OSF,
1250
      -pthread and -threads (with subtly different meanings) are
1251
      honored.  On Linux/i386, -pthread is honored.  On FreeBSD,
1252
      -pthread is honored.  Some other ports use other switches.
1253
      AFAIK, none of this is properly documented anywhere other than
1254
      in ``gcc -dumpspecs'' (look at lib and cpp entries).
1255
   
1256
 
1257
    
1258
 
1259
    
1260
      Thread Safety
1261
 
1262
 
1263
1264
We currently use the SGI STL definition of thread safety.
1265
1266
 
1267
 
1268
      The library strives to be thread-safe when all of the following
1269
         conditions are met:
1270
      
1271
      
1272
       
1273
       The system's libc is itself thread-safe,
1274
       
1275
       
1276
       
1277
         
1278
           The compiler in use reports a thread model other than
1279
           'single'. This can be tested via output from gcc
1280
           -v. Multi-thread capable versions of gcc output
1281
           something like this:
1282
         
1283
1284
%gcc -v
1285
Using built-in specs.
1286
...
1287
Thread model: posix
1288
gcc version 4.1.2 20070925 (Red Hat 4.1.2-33)
1289
1290
 
1291
Look for "Thread model" lines that aren't equal to "single."
1292
       
1293
       
1294
       
1295
         Requisite command-line flags are used for atomic operations
1296
         and threading. Examples of this include -pthread
1297
         and -march=native, although specifics vary
1298
         depending on the host environment. See 
1299
         url="http://gcc.gnu.org/onlinedocs/gcc/Option-Summary.html">Machine
1300
         Dependent Options.
1301
       
1302
       
1303
       
1304
         
1305
           An implementation of atomicity.h functions
1306
           exists for the architecture in question. See the internals documentation for more details.
1307
       
1308
       
1309
 
1310
      
1311
      The user-code must guard against concurrent method calls which may
1312
         access any particular library object's state.  Typically, the
1313
         application programmer may infer what object locks must be held
1314
         based on the objects referenced in a method call.  Without getting
1315
         into great detail, here is an example which requires user-level
1316
         locks:
1317
      
1318
      
1319
     library_class_a shared_object_a;
1320
 
1321
     thread_main () {
1322
       library_class_b *object_b = new library_class_b;
1323
       shared_object_a.add_b (object_b);   // must hold lock for shared_object_a
1324
       shared_object_a.mutate ();          // must hold lock for shared_object_a
1325
     }
1326
 
1327
     // Multiple copies of thread_main() are started in independent threads.
1328
      Under the assumption that object_a and object_b are never exposed to
1329
         another thread, here is an example that should not require any
1330
         user-level locks:
1331
      
1332
      
1333
     thread_main () {
1334
       library_class_a object_a;
1335
       library_class_b *object_b = new library_class_b;
1336
       object_a.add_b (object_b);
1337
       object_a.mutate ();
1338
     } 
1339
      All library objects are safe to use in a multithreaded program as
1340
         long as each thread carefully locks out access by any other
1341
         thread while it uses any object visible to another thread, i.e.,
1342
         treat library objects like any other shared resource.  In general,
1343
         this requirement includes both read and write access to objects;
1344
         unless otherwise documented as safe, do not assume that two threads
1345
         may access a shared standard library object at the same time.
1346
      
1347
 
1348
 
1349
  
1350
  
1351
    Atomics
1352
    
1353
    
1354
  
1355
 
1356
    
1357
      IO
1358
     This gets a bit tricky.  Please read carefully, and bear with me.
1359
   
1360
 
1361
    
1362
      Structure
1363
   A wrapper
1364
      type called __basic_file provides our abstraction layer
1365
      for the std::filebuf classes.  Nearly all decisions dealing
1366
      with actual input and output must be made in __basic_file.
1367
   
1368
   A generic locking mechanism is somewhat in place at the filebuf layer,
1369
      but is not used in the current code.  Providing locking at any higher
1370
      level is akin to providing locking within containers, and is not done
1371
      for the same reasons (see the links above).
1372
   
1373
    
1374
 
1375
    
1376
      Defaults
1377
   The __basic_file type is simply a collection of small wrappers around
1378
      the C stdio layer (again, see the link under Structure).  We do no
1379
      locking ourselves, but simply pass through to calls to fopen,
1380
      fwrite, and so forth.
1381
   
1382
   So, for 3.0, the question of "is multithreading safe for I/O"
1383
      must be answered with, "is your platform's C library threadsafe
1384
      for I/O?"  Some are by default, some are not; many offer multiple
1385
      implementations of the C library with varying tradeoffs of threadsafety
1386
      and efficiency.  You, the programmer, are always required to take care
1387
      with multiple threads.
1388
   
1389
   (As an example, the POSIX standard requires that C stdio FILE*
1390
       operations are atomic.  POSIX-conforming C libraries (e.g, on Solaris
1391
       and GNU/Linux) have an internal mutex to serialize operations on
1392
       FILE*s.  However, you still need to not do stupid things like calling
1393
       fclose(fs) in one thread followed by an access of
1394
       fs in another.)
1395
   
1396
   So, if your platform's C library is threadsafe, then your
1397
      fstream I/O operations will be threadsafe at the lowest
1398
      level.  For higher-level operations, such as manipulating the data
1399
      contained in the stream formatting classes (e.g., setting up callbacks
1400
      inside an std::ofstream), you need to guard such accesses
1401
      like any other critical shared resource.
1402
   
1403
    
1404
 
1405
    
1406
      Future
1407
    A
1408
      second choice may be available for I/O implementations:  libio.  This is
1409
      disabled by default, and in fact will not currently work due to other
1410
      issues.  It will be revisited, however.
1411
   
1412
   The libio code is a subset of the guts of the GNU libc (glibc) I/O
1413
      implementation.  When libio is in use, the __basic_file
1414
      type is basically derived from FILE.  (The real situation is more
1415
      complex than that... it's derived from an internal type used to
1416
      implement FILE.  See libio/libioP.h to see scary things done with
1417
      vtbls.)  The result is that there is no "layer" of C stdio
1418
      to go through; the filebuf makes calls directly into the same
1419
      functions used to implement fread, fwrite,
1420
      and so forth, using internal data structures.  (And when I say
1421
      "makes calls directly," I mean the function is literally
1422
      replaced by a jump into an internal function.  Fast but frightening.
1423
      *grin*)
1424
   
1425
   Also, the libio internal locks are used.  This requires pulling in
1426
      large chunks of glibc, such as a pthreads implementation, and is one
1427
      of the issues preventing widespread use of libio as the libstdc++
1428
      cstdio implementation.
1429
   
1430
   But we plan to make this work, at least as an option if not a future
1431
      default.  Platforms running a copy of glibc with a recent-enough
1432
      version will see calls from libstdc++ directly into the glibc already
1433
      installed.  For other platforms, a copy of the libio subsection will
1434
      be built and included in libstdc++.
1435
   
1436
    
1437
 
1438
    
1439
      Alternatives
1440
   Don't forget that other cstdio implementations are possible.  You could
1441
      easily write one to perform your own forms of locking, to solve your
1442
      "interesting" problems.
1443
   
1444
    
1445
 
1446
    
1447
 
1448
    
1449
      Containers
1450
 
1451
   This section discusses issues surrounding the design of
1452
      multithreaded applications which use Standard C++ containers.
1453
      All information in this section is current as of the gcc 3.0
1454
      release and all later point releases.  Although earlier gcc
1455
      releases had a different approach to threading configuration and
1456
      proper compilation, the basic code design rules presented here
1457
      were similar.  For information on all other aspects of
1458
      multithreading as it relates to libstdc++, including details on
1459
      the proper compilation of threaded code (and compatibility between
1460
      threaded and non-threaded code), see Chapter 17.
1461
   
1462
   Two excellent pages to read when working with the Standard C++
1463
      containers and threads are
1464
      SGI's
1465
      http://www.sgi.com/tech/stl/thread_safety.html and
1466
      SGI's
1467
      http://www.sgi.com/tech/stl/Allocators.html.
1468
   
1469
   However, please ignore all discussions about the user-level
1470
      configuration of the lock implementation inside the STL
1471
      container-memory allocator on those pages.  For the sake of this
1472
      discussion, libstdc++ configures the SGI STL implementation,
1473
      not you.  This is quite different from how gcc pre-3.0 worked.
1474
      In particular, past advice was for people using g++ to
1475
      explicitly define _PTHREADS or other macros or port-specific
1476
      compilation options on the command line to get a thread-safe
1477
      STL.  This is no longer required for any port and should no
1478
      longer be done unless you really know what you are doing and
1479
      assume all responsibility.
1480
   
1481
   Since the container implementation of libstdc++ uses the SGI
1482
      code, we use the same definition of thread safety as SGI when
1483
      discussing design.  A key point that beginners may miss is the
1484
      fourth major paragraph of the first page mentioned above
1485
      (For most clients...), which points out that
1486
      locking must nearly always be done outside the container, by
1487
      client code (that'd be you, not us).  There is a notable
1488
      exceptions to this rule.  Allocators called while a container or
1489
      element is constructed uses an internal lock obtained and
1490
      released solely within libstdc++ code (in fact, this is the
1491
      reason STL requires any knowledge of the thread configuration).
1492
   
1493
   For implementing a container which does its own locking, it is
1494
      trivial to provide a wrapper class which obtains the lock (as
1495
      SGI suggests), performs the container operation, and then
1496
      releases the lock.  This could be templatized to a certain
1497
      extent, on the underlying container and/or a locking
1498
      mechanism.  Trying to provide a catch-all general template
1499
      solution would probably be more trouble than it's worth.
1500
   
1501
   The library implementation may be configured to use the
1502
      high-speed caching memory allocator, which complicates thread
1503
      safety issues. For all details about how to globally override
1504
      this at application run-time
1505
      see here. Also
1506
      useful are details
1507
      on allocator
1508
      options and capabilities.
1509
   
1510
 
1511
    
1512
1513
 
1514
1515
1516
            parse="xml" href="using_exceptions.xml">
1517
1518
 
1519
1520
1521
            parse="xml" href="debug.xml">
1522
1523
 
1524

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.