-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsum_thread.cpp
43 lines (37 loc) · 891 Bytes
/
sum_thread.cpp
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
40
41
42
43
#include<pthread.h>
#include<stdio.h>
#include<thread>
#include<iostream>
#include<cstdlib>
using namespace std;
// practical 12
void *print(void *p);
int main(int argc, char **argv)
{
pthread_t tid;
pthread_attr_t attr;
pthread_attr_init(&attr);
if(argc!=2) {
cout<<"Error you have give wrong argument \n";
return -1;
}
if(atoi(argv[1])< 0) {
cout<<"\n Integer value must be greater than 0.\n";
return -1;
}
int a = atoi(argv[1]);
pthread_create(&tid , &attr , print , &a);
pthread_join(tid , NULL);
return 0;
}
void *print(void *p)
{
int i ;
int num = *(int*)(p);
int sum = 0;
cout<<"\nI am inside a thread function \n"<<endl;
for(i=1;i<=num;i++)
sum += i;
cout<<"Sum is : "<<sum<<endl<<endl;
pthread_exit(0);
}