-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8.5.c
42 lines (39 loc) · 794 Bytes
/
8.5.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
40
41
42
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
void *print_message(void *arg);
pthread_mutex_t lock;
int N;
int main (int argc, char *argv [])
{
int x=0 , ret, i=0;
pthread_t thread;
if (pthread_mutex_init(&lock, NULL) != 0)
{
printf("\n mutex init has failed\n");
return 1;
}
printf("Give me the N value:");
scanf("%d", &N);
while(i<N){
ret=pthread_create(&thread, NULL, print_message, (void*)&i);
if ( ret != 0) {
printf ("pthread_create() error\n");
return 1;
}
pthread_join(thread, NULL);
i++;
}
pthread_mutex_destroy(&lock);
return 0;
}
void *print_message(void *arg)
{
int *dat;
dat = (int *)arg;
pthread_mutex_lock(&lock);
N++;
pthread_mutex_unlock(&lock);
return 0;
}