UC3M

Telematic/Audiovisual Syst./Communication Syst. Engineering

Systems Architecture

September 2017 - January 2018

11.2.3.  Threads: Creation

#include <pthread.h>
/*
* The function returns 0 on sucess; 
*    EAGAIN with insufficient resources;
*    EINVAL with invalid settings;
*    EPERM  with no permission to set information
*/    
int pthread_create
      (pthread_t *thread, 
      const pthread_attr_t *attr,
      void *(*start_routine) (void *), 
      void *arg);

The pthread_create function creates a new thread with the following characteristics:

  • The main father and child share all information included in the heap. That includes definadas as global variables; i.e., those stored outside of all functions. These variables are shared in both reading and writing.

  • Each thread has its own stack (stack) execution. This stack is private and each thread runs through its stack. The parent thread that creates the thread resumes its execution and the new one begins running the third parameter of the function. Finally the input parameters passed to the function (void *) are passed as fourth parameter of the function.

  • The execution function returns a pointer to the result of the execution of the thread (with return or pthread_exit).

  • To work with the thread created and have access to pthread_join returned by the function requires a pointer to a pthread_t data type.

  • Finally, the attributes of threads to configure some details such as maximum heap size or the execution priority via pthread_attr_t. For POSIX threads, the operating system that is responsible for distributing the runtime on each processor.