Manual Pages for Linux CentOS command on man pthread_attr_destroy
MyWebUniversity

Manual Pages for Linux CentOS command on man pthread_attr_destroy

PTHREADATTRINIT(3) Linux Programmer's Manual PTHREADATTRINIT(3)

NAME

pthreadattrinit, pthreadattrdestroy - initialize and destroy thread attributes object SYNOPSIS

#include int pthreadattrinit(pthreadattrt *attr); int pthreadattrdestroy(pthreadattrt *attr);

Compile and link with -pthread. DESCRIPTION The pthreadattrinit() function initializes the thread attributes object pointed to by attr 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 pthreadcreate(3) calls that create threads. Calling pthreadattrinit() on a thread attributes object that has already been initialized results in undefined behavior. When a thread attributes object is no longer required, it should be destroyed using the pthreadattrdestroy() function. Destroying a thread attributes object has no effect on threads that were created using that object. Once a thread attributes object has been destroyed, it can be reini‐ tialized using pthreadattrinit(). Any other use of a destroyed thread attributes object has undefined results. RETURN VALUE On success, these functions return 0; on error, they return a nonzero error number. ERRORS

POSIX.1-2001 documents an ENOMEM error for pthreadattrinit(); on

Linux these functions always succeed (but portable and future-proof applications should nevertheless handle a possible error return). CONFORMING TO

POSIX.1-2001. NOTES The pthreadattrt type should be treated as opaque: any access to the object other than via pthreads functions is nonportable and produces undefined results. EXAMPLE The program below optionally makes use of pthreadattrinit() and vari‐ ous related functions to initialize a thread attributes object that is used to create a single thread. Once created, the thread uses the pthreadgetattrnp(3) function (a nonstandard GNU extension) to retrieve the thread's attributes, and then displays those attributes.

If the program is run with no command-line argument, then it passes NULL as the attr argument of pthreadcreate(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:

$ ulimit -s # No stack imit ==> default stack size is 2MB unlimited

$ ./a.out Thread attributes: Detach state = PTHREADCREATEJOINABLE Scope = PTHREADSCOPESYSTEM Inherit scheduler = PTHREADINHERITSCHED Scheduling policy = SCHEDOTHER Scheduling priority = 0 Guard size = 4096 bytes Stack address = 0x40196000 Stack size = 0x201000 bytes

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 pthreadcre‐

ate(3). Running the program on Linux/x86-32 with the NPTL threading implementation, we see the following:

$ ./a.out 0x3000000 posixmemalign() allocated at 0x40197000 Thread attributes: Detach state = PTHREADCREATEDETACHED Scope = PTHREADSCOPESYSTEM Inherit scheduler = PTHREADEXPLICITSCHED Scheduling policy = SCHEDOTHER Scheduling priority = 0 Guard size = 0 bytes Stack address = 0x40197000 Stack size = 0x3000000 bytes Program source

#define GNUSOURCE /* To get pthreadgetattrnp() declaration */

#include

#include

#include

#include

#include

#define handleerroren(en, msg) \ do { errno = en; perror(msg); exit(EXITFAILURE); } while (0) static void displaypthreadattr(pthreadattrt *attr, char *prefix) { int s, i; sizet v; void *stkaddr; struct schedparam sp; s = pthreadattrgetdetachstate(attr, &i); if (s != 0) handleerroren(s, "pthreadattrgetdetachstate");

printf("%sDetach state = %s\n", prefix, (i == PTHREADCREATEDETACHED) ? "PTHREADCREATEDETACHED" : (i == PTHREADCREATEJOINABLE) ? "PTHREADCREATEJOINABLE" : "???"); s = pthreadattrgetscope(attr, &i); if (s != 0) handleerroren(s, "pthreadattrgetscope");

printf("%sScope = %s\n", prefix, (i == PTHREADSCOPESYSTEM) ? "PTHREADSCOPESYSTEM" : (i == PTHREADSCOPEPROCESS) ? "PTHREADSCOPEPROCESS" : "???"); s = pthreadattrgetinheritsched(attr, &i); if (s != 0) handleerroren(s, "pthreadattrgetinheritsched");

printf("%sInherit scheduler = %s\n", prefix, (i == PTHREADINHERITSCHED) ? "PTHREADINHERITSCHED" : (i == PTHREADEXPLICITSCHED) ? "PTHREADEXPLICITSCHED" : "???"); s = pthreadattrgetschedpolicy(attr, &i); if (s != 0) handleerroren(s, "pthreadattrgetschedpolicy");

printf("%sScheduling policy = %s\n", prefix, (i == SCHEDOTHER) ? "SCHEDOTHER" : (i == SCHEDFIFO) ? "SCHEDFIFO" : (i == SCHEDRR) ? "SCHEDRR" : "???"); s = pthreadattrgetschedparam(attr, &sp); if (s != 0) handleerroren(s, "pthreadattrgetschedparam");

printf("%sScheduling priority = %d\n", prefix, sp.schedpriority); s = pthreadattrgetguardsize(attr, &v); if (s != 0) handleerroren(s, "pthreadattrgetguardsize");

printf("%sGuard size = %d bytes\n", prefix, v); s = pthreadattrgetstack(attr, &stkaddr, &v); if (s != 0) handleerroren(s, "pthreadattrgetstack");

printf("%sStack address = %p\n", prefix, stkaddr);

printf("%sStack size = 0x%x bytes\n", prefix, v); } static void * threadstart(void *arg) { int s; pthreadattrt gattr;

/* pthreadgetattrnp() is a non-standard GNU extension that retrieves the attributes of the thread specified in its first argument */ s = pthreadgetattrnp(pthreadself(), &gattr); if (s != 0) handleerroren(s, "pthreadgetattrnp"); printf("Thread attributes:\n"); displaypthreadattr(&gattr, "\t"); exit(EXITSUCCESS); /* Terminate all threads */ } int main(int argc, char *argv[]) { pthreadt thr; pthreadattrt attr; pthreadattrt *attrp; /* NULL or &attr */ int s; attrp = NULL;

/* If a command-line argument was supplied, use it to set the

stack-size attribute and set a few other thread attributes, and set attrp pointing to thread attributes object */ if (argc > 1) { int stacksize; void *sp; attrp = &attr; s = pthreadattrinit(&attr); if (s != 0) handleerroren(s, "pthreadattrinit"); s = pthreadattrsetdetachstate(&attr, PTHREADCREATEDETACHED); if (s != 0) handleerroren(s, "pthreadattrsetdetachstate"); s = pthreadattrsetinheritsched(&attr, PTHREADEXPLICITSCHED); if (s != 0) handleerroren(s, "pthreadattrsetinheritsched"); stacksize = strtoul(argv[1], NULL, 0); s = posixmemalign(&sp, sysconf(SCPAGESIZE), stacksize); if (s != 0) handleerroren(s, "posixmemalign");

printf("posixmemalign() allocated at %p\n", sp); s = pthreadattrsetstack(&attr, sp, stacksize); if (s != 0) handleerroren(s, "pthreadattrsetstack"); } s = pthreadcreate(&thr, attrp, &threadstart, NULL); if (s != 0) handleerroren(s, "pthreadcreate"); if (attrp != NULL) { s = pthreadattrdestroy(attrp); if (s != 0) handleerroren(s, "pthreadattrdestroy"); } pause(); /* Terminates when other thread calls exit() */ } SEE ALSO pthreadattrsetaffinitynp(3), pthreadattrsetdetachstate(3), pthreadattrsetguardsize(3), pthreadattrsetinheritsched(3), pthreadattrsetschedparam(3), pthreadattrsetschedpolicy(3), pthreadattrsetscope(3), pthreadattrsetstack(3), pthreadattrsetstackaddr(3), pthreadattrsetstacksize(3), pthreadcreate(3), pthreadgetattrnp(3), pthreads(7) COLOPHON

This page is part of release 3.53 of the Linux man-pages project. A description of the project, and information about reporting bugs, can

be found at http://www.kernel.org/doc/man-pages/.

Linux 2008-11-11 PTHREADATTRINIT(3)




Contact us      |      About us      |      Term of use      |       Copyright © 2000-2019 MyWebUniversity.com ™