Multithreaded using the Pthreads API

Pthreads refers to the POSIX standard (IEEE 1003.1c) defining an API for thread creation and synchronization, which is a specification for thread behavior, not an implementation. This specification may be implemented by Operating-system designers in any way they wish. The C program shown in below, demonstrates the basic Pthreads API for constructing a multithreaded program that calculates the summation of a nonnegative integer in a separate thread. Separate threads begin execution in a specified function, in a Pthreads program. In below program, this is the runner() function. When the program begins, a single thread of control begins in main(). main() creates a second thread that begins control in the runner() function, after some initialization. Both threads share the global data sum. the pthread.h header file must be included by all Pthreads programs. The identifier for the thread we will create. A set of attributes, including stack size and scheduling information are there in each thread. The attributes for the thread is represented by declaration of the pthread attr_t_attr. We set the attributes in the function call pthread attr init(&attr). Because we did not explicitly set any attributes, we use the default attributes provided. With the pthread create() function call, separate thread is created. To passing the thread identifier and the attributes for the thread, we also pass the runner() function, where the new thread will begin execution. At Last, we pass the integer parameter that was provided on the command line, argv[1]. At this point, the program has two threads: the initial (or parent) thread in main() and the summation (or child) thread performing the summation operation in the runner() function. This program follows the fork-join strategy: after creating the summation thread, the parent thread will wait for it to terminate by calling the pthread join() function. When it calls the function pthread exit(), when the summation thread will terminate .The parent thread will output the value of the shared data sum, once the summation thread has returned,

Multithreaded C program using the Pthreads API


int sum;
/* This ‘sum’ is shared by the thread(s) */
void *runner(void *param);
/* threads call this function */
int main(int argc, char *argv[]){
   pthread t tid;
   /* the thread identifier */
   pthread attr t attr;
   /* set of thread attributes */
   if (argc != 2){
      fprintf(stderr,"usage: a.out 
");       return -1;    }    if (atoi(argv[1]) < 0){       fprintf(stderr,"%d must be >= 0
",atoi(argv[1])); return -1;    }    /* get the default attributes */    pthread attr init(&attr);    /* create the thread */    pthread create(&tid,&attr,runner,argv[1]);    /* wait for the thread to exit */    pthread join(tid,NULL);    printf("sum = %d
",sum); } /* The thread will begin handle in this function */ void *runner(void *param){    int i, upper = atoi(param);    sum = 0;    for (i = 1; i <= upper; i++)       sum += i;    pthread exit(0); }

Updated on: 16-Oct-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started