Multithreaded Programming Guide


The Multithreaded Programming Guide describes the multithreaded programming interfaces for POSIX and Solaris threads in the Solaris(TM) 2.5 system. This guide shows application programmers how to create new multithreaded programs and how to add multithreading to existing programs.

Although this guide covers both the POSIX and Solaris threads implementations, most topics assume a POSIX threads interest. Information applying to only Solaris threads is covered in a special chapter.

To understand this guide, a reader must be familiar with

Ordering Sun Documents

The SunDocsSM program provides more than 250 manuals from Sun Microsystems, Inc. If you live in the United States, Canada, Europe, or Japan, you can purchase documentation sets or individual manuals using this program.

For a list of documents and how to order them, see the catalog section of SunExpress(TM) On The Internet at

How This Guide Is Organized

Chapter 1, Covering Multithreading Basics, gives a structural overview of threads implementation in this release.

Chapter 2, Basic Threads Programming, discusses the general POSIX threads library routines, emphasizing creating a thread with default attributes.

Chapter 3, Thread Create Attributes , covers creating a thread with nondefault attributes.

Chapter 4, Programming with Synchronization Objects, covers the threads library synchronization routines.

Chapter 5, Programming with the Operating Environment, discusses changes to the operating environment to support multithreading.

Chapter 6, Safe and Unsafe Interfaces , covers multithreading safety issues.

Chapter 7, Compiling and Debugging, covers the basics of compiling and debugging multithreaded applications.

Chapter 8, Tools for Enhancing MT Programs, describes some of the tools available for gathering performance and debugging information about your multithreaded programs.

Chapter 9, Programming with Solaris Threads , covers the Solaris threads (as opposed to POSIX threads) interfaces.

Chapter 10, Programming Guidelines, discusses issues that affect programmers writing multithreaded applications.

Appendix A, Sample Application - Multithreaded grep, shows how code can be designed for POSIX threads.

Appendix B, Solaris Threads Example: barrier.c, shows an example of building a barrier in Solaris threads.

Appendix C, MT Safety Levels: Library Interfaces, lists the safety levels of library routines.

You can find additional useful information about multithreaded programming by browsing the following World Wide Web (WWW) site:

What Typographic Changes and Symbols Mean

Table P-1 describes the type changes and symbols used in this guide.

Table P-1 Typographic Conventions

Typeface or Symbol 




Commands, files, directories, and C functions; code examples 

The fork1() function is new.

Use ls -a to list all files.



Variables, titles, and emphasized words 

The stack_size value is set by...

You must specify a zero value.


What you type, contrasted with on-screen computer output 

system% cc prog.c


The man page name and section in the Solaris Reference Manual

See thr_create(3T). 

Sections of program code in the main text are enclosed in boxes:

nt test (100);

 	register int a, b, c, d, e, f;

 	test(a) = b & test(c & 0x1) & test(d & 0x1);