can be either PTHREAD_CREATE_JOINABLE or PTHREAD_CREATE_DETACHED. By default, threads are created pthread_create
can be used to join with the thread and retrieve its return value, or to cancel it. If you set the
When you create threads that you know you won't need to cancel, or join with, you should create them detached. Remember that, in many cases, even if you want to know when a thread terminates, or receive some return value from it, you may not need to use pthread_join
. If you provide your own notification mechanism, for example, using a condition variable, you can still create your threads detached.
Setting the size of a stack is not very portable.
If your system defines the symbol _POSIX_THREAD_ATTR_STACKSIZE, then you can set the
Pthreads defines the symbol PTHREAD_STACK_MIN as the minimum stack size required for a thread: If you really need to specify a stack size, you might be best off calculating your requirements in terms of the minimum required by the implementation. Or, you could base your requirements on the default pthread_attr_getstacksize
.
Setting the address of a stack is less portable!
If your system defines the symbol _POSIX_THREAD_ATTR_STACKADDR, then you can set the
You also need to be aware of whether the machine increments (or decrements) the stack before or after writing a new value — this determines whether the address you specify should be 'inside' or 'outside' the stack you've allocated. The system can't tell whether you allocated enough space, or specified the right address, so it has to trust you. If you get it wrong, undesirable things will occur.
Use the
The thread_attr.c program that follows shows some of these attributes in action, with proper conditionalization to avoid using the pthread_exit
, which means that the process will terminate when the last thread exits.
This example does not include the priority scheduling attributes, which are discussed (and demonstrated) in Section 5.5.2. It also does not demonstrate use of the
¦ thread_attr.c
1 #include <limits.h>
2 #include <pthread.h>
3 #include 'errors.h'
4
5 /*
6 * Thread start routine that reports it ran, and then exits.
7 */
8 void *thread_routine (void *arg)
9 {
10 printf ('The thread is here
');
11 return NULL;
12 }
13
14 int main (int argc, char *argv[])
15 {
16 pthread_t thread_id;
17 pthread_attr_t thread_attr;
18 struct sched_param thread_param;
19 size_t stack_size;
20 int status;
21
22 status = pthread_attr_init (&thread_attr);
23 if (status != 0)
24 err_abort (status, 'Create attr');
25
26 /*
27 * Create a detached thread.
28 */
29 status = pthread_attr_setdetachstate (
30 &thread_attr, PTHREAD_CREATE_DETACHED);
31 if (status != 0)
32 err_abort (status, 'Set detach');
33 #ifdef _POSIX_THREAD_ATTR_STACKSIZE
34 /*
35 * If supported, determine the default stack size and report
36 * it, and then select a stack size for the new thread.
37 *
38 * Note that the standard does not specify the default stack
39 * size, and the default value in an attributes object need
40 * not be the size that will actually be used. Solaris 2.5
41 * uses a value of 0 to indicate the default.
42 */
43 status = pthread_attr_getstacksize (&thread_attr, &stack_size);
44 if (status != 0)
45 err_abort (status, 'Get stack size');
46 printf ('Default stack size is %u; minimum is %u
',