Threads Library Functions pthread_create(3THR)
NAME
pthread_create - create a thread
SYNOPSIS
cc -mt [ flag... ] file...- lpthread [ library... ]
#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t
*attr, void *(*start_routine, void*),void *arg);
DESCRIPTION refer-
enced fol-
lows:
o The signal mask is inherited from the creating thread.
o The set of signals pending for the new thread is
empty.);
SunOS 5.8 Last change: 15 May 1998 1
Threads Library Functions pthread_create(3THR));
To customize the attributes for POSIX threads, see
pthread_attr_init(3THR)..
RETURN VALUES
If successful, the pthread_create() function returns 0.
Otherwise, an error number is returned to indicate the
error.
ERRORS
The pthread_create() function will fail if:
ENOMEM
The system lacked the necessary resources to create
another thread.
EINVAL
The value specified by attr is invalid.
EPERM The caller does not have appropriate permission to set
the required scheduling parameters or scheduling pol-
icy.
EXAMPLES
Example 1: This is an example of concurrency with multi-
threading.
SunOS 5.8 Last change: 15 May 1998 2
Threads Library Functions pthread_create(3THR)
process is timed, the results will show that all five indi-
vidual calls to sleep for ten-seconds completed in about ten
seconds, even on a uniprocessor. If a single-threaded pro-
cess */
SunOS 5.8 Last change: 15 May 1998 3
Threads Library Functions pthread_create(3THR)
printf("main() reporting that all %d threads have terminated\n", i);
return (0);
} /* main */
void *
sleeping(void *arg)
{
int sleep_time = (int)arg;
printf("thread %d sleeping %d seconds ...\n", thr_self(), sleep_time);
sleep(sleep_time);
printf("\nthread %d awakening\n", thr_self());
return (NULL);
}
Example 2: If main() had not waited for the completion of
the other threads (using pthread_join(3THR) or
thr_join(3THR)), it would have continued to process con-
currently until it reached the end of its routine and the
entire process would have exited prematurely (see exit(2)).
ATTRIBUTES
See attributes(5) for descriptions of the following attri-
butes:
____________________________________________________________
| ATTRIBUTE TYPE | ATTRIBUTE VALUE |
|_____________________________|_____________________________|
| MT-Level | MT-Safe |
|_____________________________|_____________________________|
SEE ALSO
fork(2), sysconf(3C), pthread_attr_init(3THR),
pthread_cancel(3THR), pthread_exit(3THR),
pthread_join(3THR), attributes(5), standards(5)
NOTES
MT application threads execute independently of each other,
thus their relative behavior is unpredictable. Therefore, it
is possible for the thread executing main() to finish before
all other user application threads.
pthread_join(3THR), on the other hand, must specify the ter-
minating.
SunOS 5.8 Last change: 15 May 1998 4
Threads Library Functions pthread_create(3THR)
It is usually very difficult to determine the runtime stack
requirements for a thread. PTHREAD_STACK_MIN specifies how
much stack storage is required to execute a NULL start_func.
The total runtime requirements for stack storage are depen-
dent.
SunOS 5.8 Last change: 15 May 1998 5