Multithreaded Programming Guide

Exit Print View

Updated: July 2014
 
 

Compiling and Linking a Multithreaded Program

The Oracle Solaris Studio C compiler (cc) provides the –mt option to compile and link multithreaded code. The –mt option assures that libraries are linked in appropriate order.

The –mt option must be used consistently. If you compile with –mt and link in a separate step, you must use the –mt option in the link step as well as the compile step. If you compile and link one translation unit with –mt, you must compile and link all units of the program with –mt.

Compiling and Linking in the POSIX Threads Environment

If your application uses only Pthreads or uses both Oracle Solaris threads and Pthreads, use the following command to compile and link:

cc -mt [ flag ... ] file... [ library... ] -lpthread 

The –mt option links in the libthread library, while the –lpthread option links in the libpthread library. Both flags are needed when using Pthreads because libpthread provides an interface to libthread.

The –mt option can appear anywhere in the command line. The –lpthread option should come after any user libraries. The relative positions of –mt and –lpthread do not matter.

For example, the following lines are equivalent:

cc -mt -o myprog f1.o f2.o   -lmylib -lpthread
cc     -o myprog f1.o f2.o -mt -lmylib -lpthread
cc     -o myprog f1.o f2.o -lmylib -mt -lpthread
cc     -o myprog f1.o f2.o -lmylib -lpthread -mt

See the Oracle Solaris Studio 12.3: C User's Guide and the Oracle Solaris Studio 12.3 Command-Line Reference for more information about the cc command.

Compiling and Linking in the Oracle Solaris Threads Environment

In a Oracle Solaris threads environment, use the following options to compile and link your application:

If you application uses only Oracle Solaris threads, use the following command to compile and link:

cc -mt [ flag ... ] file... [ library... ]

The –mt option links in the libthread library.

See theOracle Solaris Studio 12.3: C User’s Guide for more information about the cc command.

Compiling and Linking in a Mixed Threads Environment

If your application uses both Pthreads and Oracle Solaris threads functions, you can compile and link with the same command used for compiling for Pthreads only:

cc -mt [ flag ... ] file... [ library... ] -lpthread 

In mixed usage, you need to include both thread.h and pthread.h.