The Sun 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.
Although the same commands can be used to compile and link in Solaris 10 and Solaris 9 releases, some differences should be considered:
In the Solaris 10 release, you do not need to explicitly link with the libthread or libpthread libraries because all the threading functions are in the libc library.
In the Solaris 9 release, the Solaris threads API and Pthreads API are contained in separate libraries, libthread and libpthread. An application compiled with the separate libraries on Solaris 9 should run on Solaris 10 because the libthread.so and libpthread.so shared objects are implemented as filters on libc.so.1. An application compiled on Solaris 10 can run on Solaris 9 if the application was explicitly linked with the libthread or libpthread libraries. Linking with -mt preserves the Solaris distinction between fork() and fork1() when the application runs on Solaris 9. Linking with the -lpthread option makes fork() behave the same way as the Solaris fork1() call in Solaris 9 and prior releases.
In the Solaris 9 release, all calls to libthread and libpthread are no-ops if the application does not link -lthread or -lpthread. The runtime library libc has many predefined libthread and libpthread stubs that are null procedures. True procedures are interposed by libthread or libpthread when the application links both libc and the thread library.
In Solaris 9 and subsequent releases, linking a non-threaded program with -mt, -lthread, or -lpthread makes no semantic difference to the program. No extra threads or extra LWPs are created. The main, and only, thread executes as a traditional single-threaded process. The only effect on the program is to make system library locks become real locks, as opposed to dummy function calls. You must pay the price of acquiring uncontended locks.
If your application uses only Pthreads or uses both 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 Sun Studio cc(1) man page and Sun Studio 12: C User’s Guide for more information about the cc command options.
In a Solaris threads environment, use the following options to compile and link your application:
If you application uses only Solaris threads, use the following command to compile and link:
cc -mt [ flag ... ] file... [ library... ] |
The -mt option links in the libthread library.
See the Sun Studio cc(1) man page and Sun Studio 12: C User’s Guide for more information about the cc command options.
If your application uses both Pthreads and 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.