-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path2_single-thread.c
39 lines (30 loc) · 968 Bytes
/
2_single-thread.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//C Program to create a thread called add, which access two integers from the user and print the sum.
// Use 'cc -pthread 2_single-thread.c' or 'gcc -pthread 2_single-thread.c' to compile
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
int global[2];
void *sumOf(void *arg)
{
int *arr;
arr = arg;
int n1,n2,sum;
n1=arr[0];
n2=arr[1];
sum = n1+n2;
printf("Sum , %d + %d = %d\n",n1,n2,sum);
return NULL;
}
int main()
{
printf("Enter First number : ");
scanf("%d",&global[0]);
printf("Enter Second number : ");
scanf("%d",&global[1]);
pthread_t add; //unsigned integer value that stores the thread id of the thread created.
pthread_create(&add,NULL,sumOf,global); //thread creation
pthread_join(add,NULL);//waiting for the termination of a thread.
printf("Thread ID: %lu ",add );//Printing thread ID
return 0;
}
// Reference : https://www.geeksforgeeks.org/thread-functions-in-c-c/