JavaScript is required to for searching.
Skip Navigation Links
Exit Print View
Oracle Solaris Studio 12.2: C++ User's Guide
search filter icon
search icon

Document Information

Preface

Part I C++ Compiler

1.  The C++ Compiler

2.  Using the C++ Compiler

3.  Using the C++ Compiler Options

Part II Writing C++ Programs

4.  Language Extensions

5.  Program Organization

6.  Creating and Using Templates

7.  Compiling Templates

8.  Exception Handling

9.  Improving Program Performance

10.  Building Multithreaded Programs

10.1 Building Multithreaded Programs

10.1.1 Indicating Multithreaded Compilation

10.1.2 Using C++ Support Libraries With Threads and Signals

10.2 Using Exceptions in a Multithreaded Program

10.2.1 Thread Cancellation

10.3 Sharing C++ Standard Library Objects Between Threads

10.4 Using Classic iostreams in a Multithreading Environment

10.4.1 Organization of the MT-Safe iostream Library

10.4.1.1 Public Conversion Routines

10.4.1.2 Compiling and Linking With the MT-Safe libC Library

10.4.1.3 MT-Safe iostream Restrictions

10.4.1.4 Reducing Performance Overhead of MT-Safe Classes

10.4.2 Interface Changes to the iostream Library

10.4.2.1 The New Classes

10.4.2.2 The New Class Hierarchy

10.4.2.3 The New Functions

10.4.3 Global and Static Data

10.4.4 Sequence Execution

10.4.5 Object Locks

10.4.5.1 Class stream_locker

10.4.6 MT-Safe Classes

10.4.7 Object Destruction

10.4.8 An Example Application

10.5 Memory Barrier Intrinsics

Part III Libraries

11.  Using Libraries

12.  Using The C++ Standard Library

13.  Using the Classic iostream Library

14.  Using the Complex Arithmetic Library

15.  Building Libraries

Part IV Appendixes

A.  C++ Compiler Options

B.  Pragmas

Glossary

Index

10.1 Building Multithreaded Programs

All libraries shipped with the C++ compiler are multithreading safe. If you want to build a multithreaded application, or if you want to link your application to a multithreaded library, you must compile and link your program with the –mt option. This option passes –D_REENTRANT to the preprocessor and passes –lthread in the correct order to ld. For compatibility mode (–compat[=4]), the –mt option ensures that libthread is linked before libC. For standard mode (the default mode), the -mt option ensures that libthread is linked before libCrun. Use of —mt is recommended as a simpler and less error-prone alternative to specifying the macro and library.

10.1.1 Indicating Multithreaded Compilation

You can check whether an application is linked to libthread or not by using the ldd command:

example% CC -mt myprog.cc
example% ldd a.out
libm.so.1 =>      /usr/lib/libm.so.1
libCrun.so.1 =>   /usr/lib/libCrun.so.1
libthread.so.1 => /usr/lib/libthread.so.1
libc.so.1 =>      /usr/lib/libc.so.1
libdl.so.1 =>     /usr/lib/libdl.so.1

10.1.2 Using C++ Support Libraries With Threads and Signals

The C++ support libraries, libCrun, libiostream, libCstd, and libC are multithread safe but are not async safe. This means that in a multithreaded application, functions available in the support libraries should not be used in signal handlers. Doing so can result in a deadlock situation.

It is not safe to use the following in a signal handler in a multithreaded application: