0% found this document useful (0 votes)
21 views1 page

Thread Arg Sum

Uploaded by

enashusingh001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views1 page

Thread Arg Sum

Uploaded by

enashusingh001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

//program to create a thread to add two numbers passed by the main process.

#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
void *thread_function(void *arg);
int num[2]={3,5};

int main()
{
pthread_t a_thread; // thread declaration
void *result;//going to store the return by the function
pthread_create(&a_thread,NULL,thread_function,(void *)num); //thread created
pthread_join(a_thread,&result); //PROCESS WAITS FOR THREAD TO FINISH.COMMENT THIS
LINE TO SEE THE DIFFERENCE.IT IS RETURNING NOTHING TO MAIN HERE SO NULL.
printf("inside main process\n");
printf("threads returns : %s\n",(char *)result);
}

void *thread_function(void *args)


{
printf("inside thread\n");
int *x =args;
int sum = x[0]+x[1];
printf("sum is %d\n",sum);
pthread_exit("sum calculated"); // returns a string to the main process

You might also like