Go to main content

Multithreaded Programming Guide

Exit Print View

Updated: November 2020

Compiling a Multithreaded Application

This section explains how to compile a multithreaded program using the Oracle Developer Studio C compiler. The Oracle Developer Studio C compiler is optimized for parallel programming and includes many features that are not available in other C compilers. See the User's Guide in the Oracle Developer Studio Library for more information about the C compiler.

Preparing for Compilation

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.

Choosing Oracle Solaris or POSIX Threads

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(7) man page for a discussion of the differences between the thread implementations. See also Programming With Oracle Solaris Threads in this guide for information about differences.

One difference between the thread types is the behavior of the fork functions.

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().

Including thread.h or pthread.h

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. When compiling multithreaded code, you should specify the –mt option to the compiler.