pthread_attr_init

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



åå
       pthread_attr_init, pthread_attr_destroy -
       ã¹ã¬ããå±æ§ãªãã¸ã§ã¯ãã®åæåã¨ç ´æ£ãè¡ã

æ¸å¼
       #include <pthread.h>

       int pthread_attr_init(pthread_attr_t *attr);
       int pthread_attr_destroy(pthread_attr_t *attr);

       -pthread ã§ã³ã³ãã¤ã«ãã¦ãªã³ã¯ããã

説æ
       pthread_attr_init() é¢æ°ã¯ attr ãæãã¹ã¬ããå±æ§ãªãã¸ã§ã¯ã (thread
       attributes object) ãããã©ã«ãã®å±æ§å¤ã§åæåããã
       ãã®å¼ã³åºãã®å¾ããªãã¸ã§ã¯ãã®åå±æ§ã¯ (é¢é£é ç®ã«è¼ã£ã¦ãã) 種ãã®
       é¢æ°ã使ã£ã¦è¨å®ãããã¨ãã§ãããã®ãªãã¸ã§ã¯ãã¯ã¹ã¬ããã®ä½æãè¡ã
       pthread_create(3) ã®å¼ã³åºãã«ããã¦ä½¿ç¨ãããã¨ãã§ãã (ä¸ã¤ã®
       ãªãã¸ã§ã¯ããè¤æ°ã® pthread_create(3) ã«æ¸¡ãã¦ããã)ã

       ãã§ã«åæåããã¦ããã¹ã¬ããå±æ§ãªãã¸ã§ã¯ãã«å¯¾ã㦠pthread_attr_init()
       ãå¼ã³åºããå ´åã ã©ã®ãããªåä½ã«ãªããã¯ä¸å®ã§ããã

       ã¹ã¬ããå±æ§ãªãã¸ã§ã¯ãããã¯ãå¿è¦ãªããªã£ãéã«ã¯ã ãã®ãªãã¸ã§ã¯ãã¯
       pthread_attr_destroy() é¢æ°ã使ã£ã¦ç ´æ£ãã¹ãã§ããã
       ã¹ã¬ããå±æ§ãªãã¸ã§ã¯ããç ´æ£ãã¦ãã
       ãã®ãªãã¸ã§ã¯ãã使ã£ã¦ä½æãããã¹ã¬ããã«ã¯å½±é¿ã¯ãªãã

       ãã£ããç ´æ£ããã¹ã¬ããå±æ§ãªãã¸ã§ã¯ãã¯ã pthread_attr_init()
       ã使ã£ã¦ååæåãããã¨ãã§ããã ç ´æ£ããã¹ã¬ããå±æ§ãªãã¸ã§ã¯ãããã以å¤ã®ç¨éã§
       使ã£ãå ´åã®çµæã¯ä¸å®ã§ããã

è¿ãå¤
       æåããã¨ããããã®é¢æ°ã¯ 0 ãè¿ãã ã¨ã©ã¼ã®å ´åã0 以å¤ã®ã¨ã©ã¼çªå·ãè¿ãã

ã¨ã©ã¼
       POSIX.1-2001 ã§ã¯ pthread_attr_init() ã«ã¯ã¨ã©ã¼ ENOMEM ã è¨è¼ããã¦ããã
       Linux ã§ã¯ããããã®é¢æ°ã¯å¸¸ã«æåãã
       (ãã ãã移æ¤æ§ãå°æ¥ãåä½ãããã¨ãä¿è¨¼ãããã¢ããªã±ã¼ã·ã§ã³ã§ã¯ æ‐
       £ã®ã¨ã©ã¼ã®è¿ãå¤ãå¦çããããã«ãã¹ãã§ãã)ã

æºæ
       POSIX.1-2001.

注æ
       pthread_attr_t åã®åé¨æ§é ã¯æèãã¹ãã§ã¯ãªãã pthreads
       é¢æ°çµç±ä»¥å¤ã§ã®ãªãã¸ã§ã¯ãã¸ã®ã¢ã¯ã»ã¹ã¯ç§»æ¤æ§ããªãã
       ã©ã®ãããªçµæãå¾ãããããåãããªãã

ä¾
       ä¸è¨ã®ããã°ã©ã ã¯ãpthread_attr_init() ã¨ç¨®ãã®é¢é£é¢æ°ã使ã£ã¦ã
       ã¹ã¬ããå±æ§ãªãã¸ã§ã¯ãã®åæåãè¡ãã ãã®ãªãã¸ã§ã¯ãã使ã£ã¦ã¹ã¬ãããä¸ã¤ä½æããã
       ä½æãããã¹ã¬ããã¯ãä½æå¾ã« pthread_getattr_np(3) é¢æ° (éæ¨æºã® GNU æ¡å¼µ)
       ã使ã£ã¦ã¹ã¬ããã®å±æ§ãåå¾ãã åå¾ããå±æ§ã表示ããã

       ã³ãã³ãã©ã¤ã³å¼ãæ°ãªãã§ããã°ã©ã ãå®è¡ããå ´åã pthread_create(3) ã® attr
       å¼ãæ°ã«ã¯ NULL ã渡ãããã ãã®å ´åãã¹ã¬ããã¯ããã©ã«ãã®å±æ§ã§ä½æãããã ãã®ãã‐
       ã°ã©ã ã NPTL ã¹ã¬ããå®è£ã使ããã¦ãã Linux/x86-32 ã§
       åä½ãããã¨ã以ä¸ã®ãããªåºåãå¾ãããã

           $ ulimit -s       # No stack imit ==> default stack size is 2MB
           unlimited
           $ ./a.out
           Thread attributes:
                   Detach state        = PTHREAD_CREATE_JOINABLE
                   Scope               = PTHREAD_SCOPE_SYSTEM
                   Inherit scheduler   = PTHREAD_INHERIT_SCHED
                   Scheduling policy   = SCHED_OTHER
                   Scheduling priority = 0
                   Guard size          = 4096 bytes
                   Stack address       = 0x40196000
                   Stack size          = 0x201000 bytes

       ã³ãã³ãã©ã¤ã³å¼ãæ°ã§ã¹ã¿ãã¯ãµã¤ãºãä¸ããããå ´åã ãã®ãã‐
       ã°ã©ã ã¯ãã¹ã¬ããå±æ§ãªãã¸ã§ã¯ããåæåãã ãã®ãªãã¸ã§ã¯ãã®å種å±æ§ãè¨å®ãã
       pthread_create(3) ã®å¼ã³åºãã§ãã®ãªãã¸ã§ã¯ãã¸ã®ãã¤ã³ã¿ã渡ãã ãã®ãã‐
       ã°ã©ã ã NPTL ã¹ã¬ããå®è£ã使ããã¦ãã Linux/x86-32 ã§
       åä½ãããã¨ã以ä¸ã®ãããªåºåãå¾ãããã

           $ ./a.out 0x3000000
           posix_memalign() allocated at 0x40197000
           Thread attributes:
                   Detach state        = PTHREAD_CREATE_DETACHED
                   Scope               = PTHREAD_SCOPE_SYSTEM
                   Inherit scheduler   = PTHREAD_EXPLICIT_SCHED
                   Scheduling policy   = SCHED_OTHER
                   Scheduling priority = 0
                   Guard size          = 0 bytes
                   Stack address       = 0x40197000
                   Stack size          = 0x3000000 bytes

   ããã°ã©ã ã®ã½ã¼ã¹

       #define _GNU_SOURCE     /* To get pthread_getattr_np() declaration */
       #include <pthread.h>
       #include <stdio.h>
       #include <stdlib.h>
       #include <unistd.h>
       #include <errno.h>

       #define handle_error_en(en, msg) \
               do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)

       static void
       display_pthread_attr(pthread_attr_t *attr, char *prefix)
       {
           int s, i;
           size_t v;
           void *stkaddr;
           struct sched_param sp;

           s = pthread_attr_getdetachstate(attr, &i);
           if (s != 0)
               handle_error_en(s, "pthread_attr_getdetachstate");
           printf("%sDetach state        = %s\n", prefix,
                   (i == PTHREAD_CREATE_DETACHED) ? "PTHREAD_CREATE_DETACHED" :
                   (i == PTHREAD_CREATE_JOINABLE) ? "PTHREAD_CREATE_JOINABLE" :
                   "???");

           s = pthread_attr_getscope(attr, &i);
           if (s != 0)
               handle_error_en(s, "pthread_attr_getscope");
           printf("%sScope               = %s\n", prefix,
                   (i == PTHREAD_SCOPE_SYSTEM)  ? "PTHREAD_SCOPE_SYSTEM" :
                   (i == PTHREAD_SCOPE_PROCESS) ? "PTHREAD_SCOPE_PROCESS" :
                   "???");

           s = pthread_attr_getinheritsched(attr, &i);
           if (s != 0)
               handle_error_en(s, "pthread_attr_getinheritsched");
           printf("%sInherit scheduler   = %s\n", prefix,
                   (i == PTHREAD_INHERIT_SCHED)  ? "PTHREAD_INHERIT_SCHED" :
                   (i == PTHREAD_EXPLICIT_SCHED) ? "PTHREAD_EXPLICIT_SCHED" :
                   "???");

           s = pthread_attr_getschedpolicy(attr, &i);
           if (s != 0)
               handle_error_en(s, "pthread_attr_getschedpolicy");
           printf("%sScheduling policy   = %s\n", prefix,
                   (i == SCHED_OTHER) ? "SCHED_OTHER" :
                   (i == SCHED_FIFO)  ? "SCHED_FIFO" :
                   (i == SCHED_RR)    ? "SCHED_RR" :
                   "???");

           s = pthread_attr_getschedparam(attr, &sp);
           if (s != 0)
               handle_error_en(s, "pthread_attr_getschedparam");
           printf("%sScheduling priority = %d\n", prefix, sp.sched_priority);

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

           s = pthread_attr_getstack(attr, &stkaddr, &v);
           if (s != 0)
               handle_error_en(s, "pthread_attr_getstack");
           printf("%sStack address       = %p\n", prefix, stkaddr);
           printf("%sStack size          = 0x%x bytes\n", prefix, v);
       }

       static void *
       thread_start(void *arg)
       {
           int s;
           pthread_attr_t gattr;

           /* pthread_getattr_np() is a non-standard GNU extension that
              retrieves the attributes of the thread specified in its
              first argument */

           s = pthread_getattr_np(pthread_self(), &gattr);
           if (s != 0)
               handle_error_en(s, "pthread_getattr_np");

           printf("Thread attributes:\n");
           display_pthread_attr(&gattr, "\t");

           exit(EXIT_SUCCESS);         /* Terminate all threads */
       }

       int
       main(int argc, char *argv[])
       {
           pthread_t thr;
           pthread_attr_t attr;
           pthread_attr_t *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 stack_size;
               void *sp;

               attrp = &attr;

               s = pthread_attr_init(&attr);
               if (s != 0)
                   handle_error_en(s, "pthread_attr_init");

               s = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
               if (s != 0)
                   handle_error_en(s, "pthread_attr_setdetachstate");

               s = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
               if (s != 0)
                   handle_error_en(s, "pthread_attr_setinheritsched");

               stack_size = strtoul(argv[1], NULL, 0);

               s = posix_memalign(&sp, sysconf(_SC_PAGESIZE), stack_size);
               if (s != 0)
                   handle_error_en(s, "posix_memalign");

               printf("posix_memalign() allocated at %p\n", sp);

               s = pthread_attr_setstack(&attr, sp, stack_size);
               if (s != 0)
                   handle_error_en(s, "pthread_attr_setstack");
           }

           s = pthread_create(&thr, attrp, &thread_start, NULL);
           if (s != 0)
               handle_error_en(s, "pthread_create");

           if (attrp != NULL) {
               s = pthread_attr_destroy(attrp);
               if (s != 0)
                   handle_error_en(s, "pthread_attr_destroy");
           }

           pause();    /* Terminates when other thread calls exit() */
       }

é¢é£é ç®
       pthread_attr_setaffinity_np(3), pthread_attr_setdetachstate(3),
       pthread_attr_setguardsize(3), pthread_attr_setinheritsched(3),
       pthread_attr_setschedparam(3), pthread_attr_setschedpolicy(3),
       pthread_attr_setscope(3), pthread_attr_setstack(3),
       pthread_attr_setstackaddr(3), pthread_attr_setstacksize(3),
       pthread_create(3), pthread_getattr_np(3), pthreads(7)

ãã®ææ¸ã«ã¤ãã¦
       ãã® man ãã¼ã¸ã¯ Linux man-pages ããã¸ã§ã¯ãã®ãªãªã¼ã¹ 3.51 ã®ä¸é¨
       ã§ãããããã¸ã§ã¯ãã®èª¬æã¨ãã°å ±åã«é¢ããæå ±ã¯
       http://www.kernel.org/doc/man-pages/ ã«æ¸ããã¦ããã



Linux                             2008-11-11              PTHREAD_ATTR_INIT(3)