1PTHREAD_ATTR_DESTROY(P)    POSIX Programmer's Manual   PTHREAD_ATTR_DESTROY(P)
2
3
4

NAME

6       pthread_attr_destroy,  pthread_attr_init  -  destroy and initialize the
7       thread attributes object
8

SYNOPSIS

10       #include <pthread.h>
11
12       int pthread_attr_destroy(pthread_attr_t *attr);
13       int pthread_attr_init(pthread_attr_t *attr);
14
15

DESCRIPTION

17       The pthread_attr_destroy() function shall destroy a  thread  attributes
18       object.  An implementation may cause pthread_attr_destroy() to set attr
19       to an implementation-defined invalid value. A destroyed attr attributes
20       object  can  be reinitialized using pthread_attr_init(); the results of
21       otherwise referencing the object after it has been destroyed are  unde‐
22       fined.
23
24       The  pthread_attr_init()  function shall initialize a thread attributes
25       object attr with the default value for all of the individual attributes
26       used by a given implementation.
27
28       The  resulting attributes object (possibly modified by setting individ‐
29       ual  attribute  values)  when  used  by  pthread_create()  defines  the
30       attributes  of  the  thread  created. A single attributes object can be
31       used in multiple simultaneous calls to pthread_create().   Results  are
32       undefined  if  pthread_attr_init() is called specifying an already ini‐
33       tialized attr attributes object.
34

RETURN VALUE

36       Upon     successful     completion,     pthread_attr_destroy()      and
37       pthread_attr_init() shall return a value of 0; otherwise, an error num‐
38       ber shall be returned to indicate the error.
39

ERRORS

41       The pthread_attr_init() function shall fail if:
42
43       ENOMEM Insufficient memory exists to initialize the  thread  attributes
44              object.
45
46
47       These functions shall not return an error code of [EINTR].
48
49       The following sections are informative.
50

EXAMPLES

52       None.
53

APPLICATION USAGE

55       None.
56

RATIONALE

58       Attributes  objects  are  provided  for threads, mutexes, and condition
59       variables as a mechanism to support probable future standardization  in
60       these areas without requiring that the function itself be changed.
61
62       Attributes  objects provide clean isolation of the configurable aspects
63       of threads. For example, "stack size" is an important  attribute  of  a
64       thread,  but  it  cannot be expressed portably. When porting a threaded
65       program, stack sizes often need to be adjusted.  The use of  attributes
66       objects  can  help  by  allowing the changes to be isolated in a single
67       place, rather than being spread across every instance  of  thread  cre‐
68       ation.
69
70       Attributes objects can be used to set up "classes' of threads with sim‐
71       ilar attributes; for example, "threads with large stacks and high  pri‐
72       ority"  or  "threads with minimal stacks". These classes can be defined
73       in a single place and then referenced wherever threads need to be  cre‐
74       ated. Changes to "class" decisions become straightforward, and detailed
75       analysis of each pthread_create() call is not required.
76
77       The attributes objects are defined as opaque types as an aid to  exten‐
78       sibility.   If  these  objects had been specified as structures, adding
79       new attributes would force recompilation of all multi-threaded programs
80       when the attributes objects are extended; this might not be possible if
81       different program components were supplied by different vendors.
82
83       Additionally,  opaque  attributes  objects  present  opportunities  for
84       improving  performance.  Argument  validity  can  be  checked once when
85       attributes are set, rather than each time a thread is created.   Imple‐
86       mentations  often  need  to  cache kernel objects that are expensive to
87       create. Opaque attributes objects provide  an  efficient  mechanism  to
88       detect when cached objects become invalid due to attribute changes.
89
90       Since  assignment  is  not  necessarily defined on a given opaque type,
91       implementation-defined default values cannot be defined in  a  portable
92       way.  The solution to this problem is to allow attributes objects to be
93       initialized dynamically by attributes object initialization  functions,
94       so that default values can be supplied automatically by the implementa‐
95       tion.
96
97       The following proposal was provided as a suggested alternative  to  the
98       supplied attributes:
99
100        1. Maintain  the  style  of passing a parameter formed by the bitwise-
101           inclusive OR of flags to the initialization routines ( pthread_cre‐
102           ate(),  pthread_mutex_init(),  pthread_cond_init()).  The parameter
103           containing the flags should be an opaque type for extensibility. If
104           no  flags  are  set  in the parameter, then the objects are created
105           with default characteristics. An implementation may specify  imple‐
106           mentation-defined flag values and associated behavior.
107
108        2. If  further  specialization  of  mutexes and condition variables is
109           necessary, implementations may specify additional  procedures  that
110           operate  on the pthread_mutex_t and pthread_cond_t objects (instead
111           of on attributes objects).
112
113       The difficulties with this solution are:
114
115        1. A bitmask is not opaque if bits  have  to  be  set  into  bitvector
116           attributes  objects  using  explicitly-coded  bitwise-inclusive  OR
117           operations. If the set of options exceeds an int, application  pro‐
118           grammers  need to know the location of each bit. If bits are set or
119           read by encapsulation (that is, get and set  functions),  then  the
120           bitmask  is  merely an implementation of attributes objects as cur‐
121           rently defined and should not be exposed to the programmer.
122
123        2. Many attributes are not Boolean or very small integral values.  For
124           example,  scheduling  policy  may  be placed in 3-bit or 4-bit, but
125           priority requires 5-bit or more, thereby taking up at least 8  bits
126           out  of  a  possible  16  bits  on  machines  with 16-bit integers.
127           Because of this, the bitmask can only  reasonably  control  whether
128           particular  attributes  are  set or not, and it cannot serve as the
129           repository of the value itself. The value needs to be specified  as
130           a  function  parameter  (which  is non-extensible), or by setting a
131           structure field (which is non-opaque), or by get and set  functions
132           (making   the  bitmask  a  redundant  addition  to  the  attributes
133           objects).
134
135       Stack size is defined as an optional attribute because the very  notion
136       of  a  stack  is inherently machine-dependent. Some implementations may
137       not be able to change the size of the stack, for  example,  and  others
138       may  not  need  to  because stack pages may be discontiguous and can be
139       allocated and released on demand.
140
141       The attribute mechanism has been designed in large measure for extensi‐
142       bility.   Future  extensions  to  the  attribute  mechanism  or  to any
143       attributes object defined in this volume of IEEE Std 1003.1-2001 has to
144       be done with care so as not to affect binary-compatibility.
145
146       Attributes  objects,  even  if allocated by means of dynamic allocation
147       functions such as malloc(), may have their size fixed at compile  time.
148       This  means,  for example, a pthread_create() in an implementation with
149       extensions to pthread_attr_t cannot  look  beyond  the  area  that  the
150       binary  application  assumes  is valid.  This suggests that implementa‐
151       tions should maintain a size field in the attributes object, as well as
152       possibly  version  information,  if  extensions in different directions
153       (possibly by different vendors) are to be accommodated.
154

FUTURE DIRECTIONS

156       None.
157

SEE ALSO

159       pthread_attr_getstackaddr()     ,     pthread_attr_getstacksize()     ,
160       pthread_attr_getdetachstate() , pthread_create() , the Base Definitions
161       volume of IEEE Std 1003.1-2001, <pthread.h>
162
164       Portions of this text are reprinted and reproduced in  electronic  form
165       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
166       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
167       Specifications  Issue  6,  Copyright  (C) 2001-2003 by the Institute of
168       Electrical and Electronics Engineers, Inc and The Open  Group.  In  the
169       event of any discrepancy between this version and the original IEEE and
170       The Open Group Standard, the original IEEE and The Open Group  Standard
171       is  the  referee document. The original Standard can be obtained online
172       at http://www.opengroup.org/unix/online.html .
173
174
175
176IEEE/The Open Group                  2003              PTHREAD_ATTR_DESTROY(P)
Impressum