412 lines
19 KiB
HTML
412 lines
19 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of PTHREAD_ATTR_INIT</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>PTHREAD_ATTR_INIT</H1>
|
|
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
pthread_attr_init, pthread_attr_destroy - initialize and destroy
|
|
thread attributes object
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/pthread.h">pthread.h</A>></B>
|
|
|
|
<B>int pthread_attr_init(pthread_attr_t *</B><I>attr</I><B>);</B>
|
|
<B>int pthread_attr_destroy(pthread_attr_t *</B><I>attr</I><B>);</B>
|
|
|
|
Compile and link with <I>-pthread</I>.
|
|
</PRE>
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
The
|
|
<B>pthread_attr_init</B>()
|
|
|
|
function initializes the thread attributes object pointed to by
|
|
<I>attr</I>
|
|
|
|
with default attribute values.
|
|
After this call, individual attributes of the object can be set
|
|
using various related functions (listed under SEE ALSO),
|
|
and then the object can be used in one or more
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_create">pthread_create</A></B>(3)
|
|
|
|
calls that create threads.
|
|
<P>
|
|
|
|
Calling
|
|
<B>pthread_attr_init</B>()
|
|
|
|
on a thread attributes object that has already been initialized
|
|
results in undefined behavior.
|
|
<P>
|
|
|
|
When a thread attributes object is no longer required,
|
|
it should be destroyed using the
|
|
<B>pthread_attr_destroy</B>()
|
|
|
|
function.
|
|
Destroying a thread attributes object has no effect
|
|
on threads that were created using that object.
|
|
<P>
|
|
|
|
Once a thread attributes object has been destroyed,
|
|
it can be reinitialized using
|
|
<B>pthread_attr_init</B>().
|
|
|
|
Any other use of a destroyed thread attributes object
|
|
has undefined results.
|
|
<A NAME="lbAE"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
On success, these functions return 0;
|
|
on error, they return a nonzero error number.
|
|
<A NAME="lbAF"> </A>
|
|
<H2>ERRORS</H2>
|
|
|
|
POSIX.1 documents an
|
|
<B>ENOMEM</B>
|
|
|
|
error for
|
|
<B>pthread_attr_init</B>();
|
|
|
|
on Linux these functions always succeed
|
|
(but portable and future-proof applications should nevertheless
|
|
handle a possible error return).
|
|
<A NAME="lbAG"> </A>
|
|
<H2>ATTRIBUTES</H2>
|
|
|
|
For an explanation of the terms used in this section, see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
|
|
|
|
|
|
<TABLE BORDER>
|
|
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
|
|
<TR VALIGN=top><TD>
|
|
<B>pthread_attr_init</B>(),
|
|
|
|
<B>pthread_attr_destroy</B>()
|
|
|
|
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
|
|
</TABLE>
|
|
|
|
|
|
<A NAME="lbAH"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
POSIX.1-2001, POSIX.1-2008.
|
|
<A NAME="lbAI"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
The
|
|
<I>pthread_attr_t</I>
|
|
|
|
type should be treated as opaque:
|
|
any access to the object other than via pthreads functions
|
|
is nonportable and produces undefined results.
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
The program below optionally makes use of
|
|
<B>pthread_attr_init</B>()
|
|
|
|
and various related functions to initialize a thread attributes
|
|
object that is used to create a single thread.
|
|
Once created, the thread uses the
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_getattr_np">pthread_getattr_np</A></B>(3)
|
|
|
|
function (a nonstandard GNU extension) to retrieve the thread's
|
|
attributes, and then displays those attributes.
|
|
<P>
|
|
|
|
If the program is run with no command-line argument,
|
|
then it passes NULL as the
|
|
<I>attr</I>
|
|
|
|
argument of
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_create">pthread_create</A></B>(3),
|
|
|
|
so that the thread is created with default attributes.
|
|
Running the program on Linux/x86-32 with the NPTL threading implementation,
|
|
we see the following:
|
|
<P>
|
|
|
|
|
|
|
|
|
|
$<B> ulimit -s</B> # No stack limit ==> default stack size is 2 MB
|
|
|
|
unlimited
|
|
$<B> ./a.out</B>
|
|
|
|
Thread attributes:
|
|
<BR> Detach state = PTHREAD_CREATE_JOINABLE
|
|
<BR> Scope = PTHREAD_SCOPE_SYSTEM
|
|
<BR> Inherit scheduler = PTHREAD_INHERIT_SCHED
|
|
<BR> Scheduling policy = SCHED_OTHER
|
|
<BR> Scheduling priority = 0
|
|
<BR> Guard size = 4096 bytes
|
|
<BR> Stack address = 0x40196000
|
|
<BR> Stack size = 0x201000 bytes
|
|
|
|
|
|
<P>
|
|
|
|
When we supply a stack size as a command-line argument,
|
|
the program initializes a thread attributes object,
|
|
sets various attributes in that object,
|
|
and passes a pointer to the object in the call to
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_create">pthread_create</A></B>(3).
|
|
|
|
Running the program on Linux/x86-32 with the NPTL threading implementation,
|
|
we see the following:
|
|
<P>
|
|
|
|
|
|
|
|
|
|
$<B> ./a.out 0x3000000</B>
|
|
|
|
posix_memalign() allocated at 0x40197000
|
|
Thread attributes:
|
|
<BR> Detach state = PTHREAD_CREATE_DETACHED
|
|
<BR> Scope = PTHREAD_SCOPE_SYSTEM
|
|
<BR> Inherit scheduler = PTHREAD_EXPLICIT_SCHED
|
|
<BR> Scheduling policy = SCHED_OTHER
|
|
<BR> Scheduling priority = 0
|
|
<BR> Guard size = 0 bytes
|
|
<BR> Stack address = 0x40197000
|
|
<BR> Stack size = 0x3000000 bytes
|
|
|
|
|
|
<A NAME="lbAK"> </A>
|
|
<H3>Program source</H3>
|
|
|
|
|
|
|
|
#define _GNU_SOURCE /* To get pthread_getattr_np() declaration */
|
|
#include <<A HREF="file:///usr/include/pthread.h">pthread.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>>
|
|
#include <<A HREF="file:///usr/include/errno.h">errno.h</A>>
|
|
<P>
|
|
#define handle_error_en(en, msg) \
|
|
<BR> do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
|
|
<P>
|
|
static void
|
|
display_pthread_attr(pthread_attr_t *attr, char *prefix)
|
|
{
|
|
<BR> int s, i;
|
|
<BR> size_t v;
|
|
<BR> void *stkaddr;
|
|
<BR> struct sched_param sp;
|
|
<P>
|
|
<BR> s = pthread_attr_getdetachstate(attr, &i);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_attr_getdetachstate");
|
|
<BR> printf("%sDetach state = %s\n", prefix,
|
|
<BR> (i == PTHREAD_CREATE_DETACHED) ? "PTHREAD_CREATE_DETACHED" :
|
|
<BR> (i == PTHREAD_CREATE_JOINABLE) ? "PTHREAD_CREATE_JOINABLE" :
|
|
<BR> "???");
|
|
<P>
|
|
<BR> s = pthread_attr_getscope(attr, &i);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_attr_getscope");
|
|
<BR> printf("%sScope = %s\n", prefix,
|
|
<BR> (i == PTHREAD_SCOPE_SYSTEM) ? "PTHREAD_SCOPE_SYSTEM" :
|
|
<BR> (i == PTHREAD_SCOPE_PROCESS) ? "PTHREAD_SCOPE_PROCESS" :
|
|
<BR> "???");
|
|
<P>
|
|
<BR> s = pthread_attr_getinheritsched(attr, &i);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_attr_getinheritsched");
|
|
<BR> printf("%sInherit scheduler = %s\n", prefix,
|
|
<BR> (i == PTHREAD_INHERIT_SCHED) ? "PTHREAD_INHERIT_SCHED" :
|
|
<BR> (i == PTHREAD_EXPLICIT_SCHED) ? "PTHREAD_EXPLICIT_SCHED" :
|
|
<BR> "???");
|
|
<P>
|
|
<BR> s = pthread_attr_getschedpolicy(attr, &i);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_attr_getschedpolicy");
|
|
<BR> printf("%sScheduling policy = %s\n", prefix,
|
|
<BR> (i == SCHED_OTHER) ? "SCHED_OTHER" :
|
|
<BR> (i == SCHED_FIFO) ? "SCHED_FIFO" :
|
|
<BR> (i == SCHED_RR) ? "SCHED_RR" :
|
|
<BR> "???");
|
|
<P>
|
|
<BR> s = pthread_attr_getschedparam(attr, &sp);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_attr_getschedparam");
|
|
<BR> printf("%sScheduling priority = %d\n", prefix, sp.sched_priority);
|
|
<P>
|
|
<BR> s = pthread_attr_getguardsize(attr, &v);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_attr_getguardsize");
|
|
<BR> printf("%sGuard size = %zu bytes\n", prefix, v);
|
|
<P>
|
|
<BR> s = pthread_attr_getstack(attr, &stkaddr, &v);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_attr_getstack");
|
|
<BR> printf("%sStack address = %p\n", prefix, stkaddr);
|
|
<BR> printf("%sStack size = 0x%zx bytes\n", prefix, v);
|
|
}
|
|
<P>
|
|
static void *
|
|
thread_start(void *arg)
|
|
{
|
|
<BR> int s;
|
|
<BR> pthread_attr_t gattr;
|
|
<P>
|
|
<BR> /* pthread_getattr_np() is a non-standard GNU extension that
|
|
<BR> retrieves the attributes of the thread specified in its
|
|
<BR> first argument */
|
|
<P>
|
|
<BR> s = pthread_getattr_np(pthread_self(), &gattr);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_getattr_np");
|
|
<P>
|
|
<BR> printf("Thread attributes:\n");
|
|
<BR> display_pthread_attr(&gattr, "\t");
|
|
<P>
|
|
<BR> exit(EXIT_SUCCESS); /* Terminate all threads */
|
|
}
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
<BR> pthread_t thr;
|
|
<BR> pthread_attr_t attr;
|
|
<BR> pthread_attr_t *attrp; /* NULL or &attr */
|
|
<BR> int s;
|
|
<P>
|
|
<BR> attrp = NULL;
|
|
<P>
|
|
<BR> /* If a command-line argument was supplied, use it to set the
|
|
<BR> stack-size attribute and set a few other thread attributes,
|
|
<BR> and set attrp pointing to thread attributes object */
|
|
<P>
|
|
<BR> if (argc > 1) {
|
|
<BR> int stack_size;
|
|
<BR> void *sp;
|
|
<P>
|
|
<BR> attrp = &attr;
|
|
<P>
|
|
<BR> s = pthread_attr_init(&attr);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_attr_init");
|
|
<P>
|
|
<BR> s = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_attr_setdetachstate");
|
|
<P>
|
|
<BR> s = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_attr_setinheritsched");
|
|
<P>
|
|
<BR> stack_size = strtoul(argv[1], NULL, 0);
|
|
<P>
|
|
<BR> s = posix_memalign(&sp, sysconf(_SC_PAGESIZE), stack_size);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "posix_memalign");
|
|
<P>
|
|
<BR> printf("posix_memalign() allocated at %p\n", sp);
|
|
<P>
|
|
<BR> s = pthread_attr_setstack(&attr, sp, stack_size);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_attr_setstack");
|
|
<BR> }
|
|
<P>
|
|
<BR> s = pthread_create(&thr, attrp, &thread_start, NULL);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_create");
|
|
<P>
|
|
<BR> if (attrp != NULL) {
|
|
<BR> s = pthread_attr_destroy(attrp);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_attr_destroy");
|
|
<BR> }
|
|
<P>
|
|
<BR> pause(); /* Terminates when other thread calls exit() */
|
|
}
|
|
|
|
<A NAME="lbAL"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_attr_setaffinity_np">pthread_attr_setaffinity_np</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_attr_setdetachstate">pthread_attr_setdetachstate</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_attr_setguardsize">pthread_attr_setguardsize</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_attr_setinheritsched">pthread_attr_setinheritsched</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_attr_setschedparam">pthread_attr_setschedparam</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_attr_setschedpolicy">pthread_attr_setschedpolicy</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_attr_setscope">pthread_attr_setscope</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_attr_setstack">pthread_attr_setstack</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_attr_setstackaddr">pthread_attr_setstackaddr</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_attr_setstacksize">pthread_attr_setstacksize</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_create">pthread_create</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_getattr_np">pthread_getattr_np</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_setattr_default_np">pthread_setattr_default_np</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+pthreads">pthreads</A></B>(7)
|
|
|
|
<A NAME="lbAM"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="1"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="2"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="3"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="4"><A HREF="#lbAE">RETURN VALUE</A><DD>
|
|
<DT id="5"><A HREF="#lbAF">ERRORS</A><DD>
|
|
<DT id="6"><A HREF="#lbAG">ATTRIBUTES</A><DD>
|
|
<DT id="7"><A HREF="#lbAH">CONFORMING TO</A><DD>
|
|
<DT id="8"><A HREF="#lbAI">NOTES</A><DD>
|
|
<DT id="9"><A HREF="#lbAJ">EXAMPLE</A><DD>
|
|
<DL>
|
|
<DT id="10"><A HREF="#lbAK">Program source</A><DD>
|
|
</DL>
|
|
<DT id="11"><A HREF="#lbAL">SEE ALSO</A><DD>
|
|
<DT id="12"><A HREF="#lbAM">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:52 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|