share/mk/: Fix includes
[man-pages.git] / man3 / pthread_attr_init.3
blobdfd5ac67fddbaf7b1057a693e7f6da23cc196f39
1 '\" t
2 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
3 .\"     <mtk.manpages@gmail.com>
4 .\"
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\"
7 .TH pthread_attr_init 3 (date) "Linux man-pages (unreleased)"
8 .SH NAME
9 pthread_attr_init, pthread_attr_destroy \- initialize and destroy
10 thread attributes object
11 .SH LIBRARY
12 POSIX threads library
13 .RI ( libpthread ", " \-lpthread )
14 .SH SYNOPSIS
15 .nf
16 .B #include <pthread.h>
18 .BI "int pthread_attr_init(pthread_attr_t *" attr );
19 .BI "int pthread_attr_destroy(pthread_attr_t *" attr );
20 .fi
21 .SH DESCRIPTION
22 The
23 .BR pthread_attr_init ()
24 function initializes the thread attributes object pointed to by
25 .I attr
26 with default attribute values.
27 After this call, individual attributes of the object can be set
28 using various related functions (listed under SEE ALSO),
29 and then the object can be used in one or more
30 .BR pthread_create (3)
31 calls that create threads.
33 Calling
34 .BR pthread_attr_init ()
35 on a thread attributes object that has already been initialized
36 results in undefined behavior.
38 When a thread attributes object is no longer required,
39 it should be destroyed using the
40 .BR pthread_attr_destroy ()
41 function.
42 Destroying a thread attributes object has no effect
43 on threads that were created using that object.
45 Once a thread attributes object has been destroyed,
46 it can be reinitialized using
47 .BR pthread_attr_init ().
48 Any other use of a destroyed thread attributes object
49 has undefined results.
50 .SH RETURN VALUE
51 On success, these functions return 0;
52 on error, they return a nonzero error number.
53 .SH ERRORS
54 POSIX.1 documents an
55 .B ENOMEM
56 error for
57 .BR pthread_attr_init ();
58 on Linux these functions always succeed
59 (but portable and future-proof applications should nevertheless
60 handle a possible error return).
61 .SH ATTRIBUTES
62 For an explanation of the terms used in this section, see
63 .BR attributes (7).
64 .TS
65 allbox;
66 lbx lb lb
67 l l l.
68 Interface       Attribute       Value
70 .na
71 .nh
72 .BR pthread_attr_init (),
73 .BR pthread_attr_destroy ()
74 T}      Thread safety   MT-Safe
75 .TE
76 .SH STANDARDS
77 POSIX.1-2008.
78 .SH HISTORY
79 POSIX.1-2001.
80 .SH NOTES
81 The
82 .I pthread_attr_t
83 type should be treated as opaque:
84 any access to the object other than via pthreads functions
85 is nonportable and produces undefined results.
86 .SH EXAMPLES
87 The program below optionally makes use of
88 .BR pthread_attr_init ()
89 and various related functions to initialize a thread attributes
90 object that is used to create a single thread.
91 Once created, the thread uses the
92 .BR pthread_getattr_np (3)
93 function (a nonstandard GNU extension) to retrieve the thread's
94 attributes, and then displays those attributes.
96 If the program is run with no command-line argument,
97 then it passes NULL as the
98 .I attr
99 argument of
100 .BR pthread_create (3),
101 so that the thread is created with default attributes.
102 Running the program on Linux/x86-32 with the NPTL threading implementation,
103 we see the following:
105 .in +4n
107 .\" Results from glibc 2.8, SUSE 11.0; Oct 2008
108 .RB "$" " ulimit \-s" "       # No stack limit ==> default stack size is 2 MB"
109 unlimited
110 .RB "$" " ./a.out"
111 Thread attributes:
112         Detach state        = PTHREAD_CREATE_JOINABLE
113         Scope               = PTHREAD_SCOPE_SYSTEM
114         Inherit scheduler   = PTHREAD_INHERIT_SCHED
115         Scheduling policy   = SCHED_OTHER
116         Scheduling priority = 0
117         Guard size          = 4096 bytes
118         Stack address       = 0x40196000
119         Stack size          = 0x201000 bytes
123 When we supply a stack size as a command-line argument,
124 the program initializes a thread attributes object,
125 sets various attributes in that object,
126 and passes a pointer to the object in the call to
127 .BR pthread_create (3).
128 Running the program on Linux/x86-32 with the NPTL threading implementation,
129 we see the following:
131 .in +4n
133 .\" Results from glibc 2.8, SUSE 11.0; Oct 2008
134 .RB "$" " ./a.out 0x3000000"
135 posix_memalign() allocated at 0x40197000
136 Thread attributes:
137         Detach state        = PTHREAD_CREATE_DETACHED
138         Scope               = PTHREAD_SCOPE_SYSTEM
139         Inherit scheduler   = PTHREAD_EXPLICIT_SCHED
140         Scheduling policy   = SCHED_OTHER
141         Scheduling priority = 0
142         Guard size          = 0 bytes
143         Stack address       = 0x40197000
144         Stack size          = 0x3000000 bytes
147 .SS Program source
149 .\" SRC BEGIN (pthread_attr_init.c)
151 #define _GNU_SOURCE     /* To get pthread_getattr_np() declaration */
152 #include <err.h>
153 #include <errno.h>
154 #include <pthread.h>
155 #include <stdio.h>
156 #include <stdlib.h>
157 #include <unistd.h>
159 static void
160 display_pthread_attr(pthread_attr_t *attr, char *prefix)
162     int s, i;
163     size_t v;
164     void *stkaddr;
165     struct sched_param sp;
167     s = pthread_attr_getdetachstate(attr, &i);
168     if (s != 0)
169         errc(EXIT_FAILURE, s, "pthread_attr_getdetachstate");
170     printf("%sDetach state        = %s\en", prefix,
171            (i == PTHREAD_CREATE_DETACHED) ? "PTHREAD_CREATE_DETACHED" :
172            (i == PTHREAD_CREATE_JOINABLE) ? "PTHREAD_CREATE_JOINABLE" :
173            "???");
175     s = pthread_attr_getscope(attr, &i);
176     if (s != 0)
177         errc(EXIT_FAILURE, s, "pthread_attr_getscope");
178     printf("%sScope               = %s\en", prefix,
179            (i == PTHREAD_SCOPE_SYSTEM)  ? "PTHREAD_SCOPE_SYSTEM" :
180            (i == PTHREAD_SCOPE_PROCESS) ? "PTHREAD_SCOPE_PROCESS" :
181            "???");
183     s = pthread_attr_getinheritsched(attr, &i);
184     if (s != 0)
185         errc(EXIT_FAILURE, s, "pthread_attr_getinheritsched");
186     printf("%sInherit scheduler   = %s\en", prefix,
187            (i == PTHREAD_INHERIT_SCHED)  ? "PTHREAD_INHERIT_SCHED" :
188            (i == PTHREAD_EXPLICIT_SCHED) ? "PTHREAD_EXPLICIT_SCHED" :
189            "???");
191     s = pthread_attr_getschedpolicy(attr, &i);
192     if (s != 0)
193         errc(EXIT_FAILURE, s, "pthread_attr_getschedpolicy");
194     printf("%sScheduling policy   = %s\en", prefix,
195            (i == SCHED_OTHER) ? "SCHED_OTHER" :
196            (i == SCHED_FIFO)  ? "SCHED_FIFO" :
197            (i == SCHED_RR)    ? "SCHED_RR" :
198            "???");
200     s = pthread_attr_getschedparam(attr, &sp);
201     if (s != 0)
202         errc(EXIT_FAILURE, s, "pthread_attr_getschedparam");
203     printf("%sScheduling priority = %d\en", prefix, sp.sched_priority);
205     s = pthread_attr_getguardsize(attr, &v);
206     if (s != 0)
207         errc(EXIT_FAILURE, s, "pthread_attr_getguardsize");
208     printf("%sGuard size          = %zu bytes\en", prefix, v);
210     s = pthread_attr_getstack(attr, &stkaddr, &v);
211     if (s != 0)
212         errc(EXIT_FAILURE, s, "pthread_attr_getstack");
213     printf("%sStack address       = %p\en", prefix, stkaddr);
214     printf("%sStack size          = %#zx bytes\en", prefix, v);
217 static void *
218 thread_start(void *arg)
220     int s;
221     pthread_attr_t gattr;
223     /* pthread_getattr_np() is a non\-standard GNU extension that
224        retrieves the attributes of the thread specified in its
225        first argument. */
227     s = pthread_getattr_np(pthread_self(), &gattr);
228     if (s != 0)
229         errc(EXIT_FAILURE, s, "pthread_getattr_np");
231     printf("Thread attributes:\en");
232     display_pthread_attr(&gattr, "\et");
234     exit(EXIT_SUCCESS);         /* Terminate all threads */
238 main(int argc, char *argv[])
240     pthread_t thr;
241     pthread_attr_t attr;
242     pthread_attr_t *attrp;      /* NULL or &attr */
243     int s;
245     attrp = NULL;
247     /* If a command\-line argument was supplied, use it to set the
248        stack\-size attribute and set a few other thread attributes,
249        and set attrp pointing to thread attributes object. */
251     if (argc > 1) {
252         size_t stack_size;
253         void *sp;
255         attrp = &attr;
257         s = pthread_attr_init(&attr);
258         if (s != 0)
259             errc(EXIT_FAILURE, s, "pthread_attr_init");
261         s = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
262         if (s != 0)
263             errc(EXIT_FAILURE, s, "pthread_attr_setdetachstate");
265         s = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
266         if (s != 0)
267             errc(EXIT_FAILURE, s, "pthread_attr_setinheritsched");
269         stack_size = strtoul(argv[1], NULL, 0);
271         s = posix_memalign(&sp, sysconf(_SC_PAGESIZE), stack_size);
272         if (s != 0)
273             errc(EXIT_FAILURE, s, "posix_memalign");
275         printf("posix_memalign() allocated at %p\en", sp);
277         s = pthread_attr_setstack(&attr, sp, stack_size);
278         if (s != 0)
279             errc(EXIT_FAILURE, s, "pthread_attr_setstack");
280     }
282     s = pthread_create(&thr, attrp, &thread_start, NULL);
283     if (s != 0)
284         errc(EXIT_FAILURE, s, "pthread_create");
286     if (attrp != NULL) {
287         s = pthread_attr_destroy(attrp);
288         if (s != 0)
289             errc(EXIT_FAILURE, s, "pthread_attr_destroy");
290     }
292     pause();    /* Terminates when other thread calls exit() */
295 .\" SRC END
296 .SH SEE ALSO
297 .ad l
299 .BR pthread_attr_setaffinity_np (3),
300 .BR pthread_attr_setdetachstate (3),
301 .BR pthread_attr_setguardsize (3),
302 .BR pthread_attr_setinheritsched (3),
303 .BR pthread_attr_setschedparam (3),
304 .BR pthread_attr_setschedpolicy (3),
305 .BR pthread_attr_setscope (3),
306 .BR pthread_attr_setsigmask_np (3),
307 .BR pthread_attr_setstack (3),
308 .BR pthread_attr_setstackaddr (3),
309 .BR pthread_attr_setstacksize (3),
310 .BR pthread_create (3),
311 .BR pthread_getattr_np (3),
312 .BR pthread_setattr_default_np (3),
313 .BR pthreads (7)