More on pthread_create( )

  • pthread_create( ):

      int pthread_create(pthread_t *threadID, const pthread_attr_t *attr,
                         void *(*start_routine) (void *), void *arg);
    
      Effect:  create a new thread that starts executing 
               in the function start_routine
    
      Return value:   0 --> success
                    ≠ 0 --> error code
    
      Parameters:
    
        threadID        contains the ID of the new thread upon return
        attr            specify the attributes for the new thread (later)
        start_routine   start function for the new thread
        arg             parameter for the start_routine (later)
    

  • Typical way to used pthread_create( ):

        if ( pthread_create(&tid, &attr, startFunction, &x) != 0 )
        {
            perror("pthread_create"); // Print cause of error
            exit(1);
        }
    

Specifying thread attributes when a thread is created

  • The attributes for a newly created thread can be speficied using a variable of the type pthread_attr_t:

       pthread_attr_t    attr;    
    

  • You must first reset all the thread properties to their default values using the function pthread_attr_init():

       pthread_attr_init( &attr );  // Pass the var attr by reference
    

  • You can create a thread with the default attribute values with:

         pthread_attr_t    attr;  
         pthread_attr_init( &attr );
         pthread_create(&tid, &attr, worker, NULL);
    
     or:
         pthread_create(&tid, NULL, worker, NULL);
    

Attributes of threads

  • Pthreads can be created with different attributes

  • Attibutes of a thread that can be changed:

    Attribute Default value Meaning
    contenstionscope PTHREAD_SCOPE_PROCESS Thread competes for resources within the process
    detachstate PTHREAD_CREATE_JOINABLE Thread is joinable by other threads
    stackaddr NULL Stack used by thread is allocated by the OS
    stacksize NULL Use default size (8 MB) determined by the OS
    priority 0 Priority to run (lower value is a higher priority)
    schedulingpolicy SCHED_OTHER The scheduling policy is determined by the system
    inheritsched PTHREAD_EXPLICIT_SCHED scheduling policy and parameters are not inherited but is explicitly defined by the attribute object
    guardsize PAGESIZE size of guard area for a thread's stack (this area will help determine if the thread has exceeded the total amount of stack space that was allocated for the thread)

  • Comment:

    • You mostly want to use the default value

    • You may want to change the stacksize if the thread executes a recursive function....

Changing some attributes of a thread

  • After initializing the thread attribute object attr with the default values, you can change some attribute value using:

    • pthread_attr_setscope(): change the contentionscope
    • pthread_attr_setdetachstate(): change the detachstate
    • pthread_attr_setstackaddr(): change the stack location
    • pthread_attr_setstacksize(): change the stack size
    • .. etc

  • For more information:

    • Use this command to find the pthread_attr_set...( ) functions:

      • man pthread_attr_init

    • Use this command to find the details on how to use a specific function pthread_attr_setFunName:

      • man pthread_attr_setFunName

How to change the stacksize of a thread

  • Default stack size = 8 M Bytes (see demo program)

  • When to use a larger stack:

    • Function executed by a thread contains an array variable

    • Function executed by a thread is recursive

  • How to change the stack size in attr:

      pthread_attr_t attr;      
    
      pthread_attr_init(&attr);                   // Init to default values
    
      pthread_attr_setstacksize(&attr, 10000000); // New size = 10000000 bytes  
    

  • Create the thread with the new stack size:

        pthread_create(&tid, &attr, worker, NULL)...
    

DEMO: demo/pthread/stack-size.c