Sun Microsystems, Inc.
spacerspacer
spacer www.sun.com docs.sun.com |
spacer
black dot
 
 
  Previous   Contents   Next 
   
 
Chapter 2

Basic Threads Programming

The Threads Library

This chapter introduces the basic threads programming routines from the POSIX threads library, libpthread(3THR). This chapter covers default threads, or threads with default attribute values, which are the kind of threads that are most often used in multithreaded programming.

Chapter 3, Thread Create Attributes, explains how to create and use threads with nondefault attributes.

The POSIX (libpthread) routines introduced here have programming interfaces that are similar to the original (libthread) Solaris multithreading library.

The following brief roadmap directs you to the discussion of a particular task and its associated man page.

Create a Default Thread

When an attribute object is not specified, it is NULL, and the default thread is created with the following attributes:

  • Unbound

  • Nondetached

  • With a default stack and stack size

  • With a priority of zero

You can also create a default attribute object with pthread_attr_init(), and then use this attribute object to create a default thread. See the section "Initialize Attributes" for details.

pthread_create(3THR)

Use pthread_create(3THR) to add a new thread of control to the current process.

Prototype:
int	pthread_create(pthread_t *tid, const pthread_attr_t *tattr,
    void*(*start_routine)(void *), void *arg);
#include <pthread.h>

pthread_attr_t ()tattr;
pthread_t tid;
extern void *start_routine(void *arg);
void *arg;
int ret; 

/* default behavior*/
ret = pthread_create(&tid, NULL, start_routine, arg);

/* initialized with default attributes */
ret = pthread_attr_init(&tattr);
/* default behavior specified*/
ret = pthread_create(&tid, &tattr, start_routine, arg);

The pthread_create() function is called with attr having the necessary state behavior. start_routine is the function with which the new thread begins execution. When start_routine returns, the thread exits with the exit status set to the value returned by start_routine (see "pthread_create(3THR)").

 
 
 
  Previous   Contents   Next