Skip Navigation Links | |
Exit Print View | |
Multithreaded Programming Guide Oracle Solaris 11.1 Information Library |
1. Covering Multithreading Basics
4. Programming with Synchronization Objects
5. Programming With the Oracle Solaris Software
6. Programming With Oracle Solaris Threads
Setting Up the Oracle Solaris Environment for Developing Multithreaded Applications
Compiling a Multithreaded Application
Choosing Oracle Solaris or POSIX Threads
Including <thread.h> or <pthread.h>
Compiling and Linking a Multithreaded Program
Compiling and Linking in the POSIX Threads Environment
Compiling and Linking in the Oracle Solaris Threads Environment
Debugging a Multithreaded Program
Common Oversights in Multithreaded Programs
Tracing and Debugging with DTrace
Profiling with Performance Analyzer
Detecting Data Races and Deadlocks Using Thread Analyzer
Tracing and Debugging With the TNF Utilities
This section explains how to compile a multithreaded program using the Oracle Solaris Studio C compiler. The Oracle Solaris Studio C compiler is optimized for parallel programming and includes many features that are not available in other C compilers. See the Oracle Solaris Studio 12.3: C User’s Guide for more information about the C compiler.
Your application must include <thread.h> for Oracle Solaris threads and <pthread.h> for POSIX threads. You should include the appropriate file for the API you are using, or both files if your application uses both thread APIs. See the pthread.h(3HEAD) man page for more information. The application must also include <errno.h>, <limits.h>, <signal.h> , <unistd.h> files.
The Oracle Solaris implementation of Pthreads is completely compatible with Oracle Solaris threads. You can use both Oracle Solaris threads and Pthreads in the same application. See the pthreads(5) man page for a discussion of the differences between the thread implementations. See also Chapter 6, Programming With Oracle Solaris Threads in this manual for information about differences.
One difference between the thread types is the behavior of the fork functions.
In the Solaris 9 release, the behavior of the fork() function depended on whether or not the application was linked with the POSIX threads library. When linked with -lthread (Oracle Solaris Threads) but not linked with -lpthread (POSIX Threads), fork() would duplicate in the child thread all the threads from the parent process. When the application was linked with -lpthread, whether or not also linked with -lthread, fork() was the same as fork1() and only the calling thread is duplicated.
Starting in the Oracle Solaris 10 release, a call to the forkall() function replicates in the child process all of the threads in the parent process. A call to fork1() replicates only the calling thread in the child process. In the Oracle Solaris 10 release, a call to fork() is identical to a call to fork1(); only the calling thread is replicated in the child process. This is the POSIX-specified behavior for fork(). Applications that require replicate-all fork semantics must call forkall().
The include file <thread.h> contains declarations for the Oracle Solaris threads functions. To call any Oracle Solaris thread functions, your program needs to include <thread.h>. This file enables you to produce compiled code that is compatible with earlier releases of the Oracle Solaris software.
The include file <pthread.h> contains declarations for the Pthreads functions and is required if your program uses Pthreads.
You can mix Oracle Solaris threads and POSIX threads in the same application by including both <thread.h> and <pthread.h> in the application. Then when linking and compiling you need to specify the -lpthread flag to link in the pthread APIs.
When using -mt, the Oracle Solaris threads APIs will be linked automatically. Always use the -mt option instead of listing -lthread explicitly. To use Pthreads, specify the -mt option and -lpthread option on the link command line. The libpthread library provides an interface to libthread, so you still need libthread when using Pthreads.
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.
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.
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.
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.
The Oracle Solaris semaphore routines, sema_*(3C), are contained in the standard C library. By contrast, you link with the -lrt library to get the standard sem_*(3RT) POSIX semaphore routines described in Synchronization With Semaphores.