-
Notifications
You must be signed in to change notification settings - Fork 0
/
pthread_example.c
93 lines (72 loc) · 1.61 KB
/
pthread_example.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
void vla_test(unsigned int length);
void *some_function(void *arg);
void *test_static_variable_with_threads(void *arg);
int main(void)
{
pthread_t thread_id1, thread_id2, thread_id3;
void *exit_status1, *exit_status2, *exit_status3;
int value1, value2, value3;
char input;
unsigned int array_length=0;
value1 = 42;
value2 = 0;
//fork();
pthread_create(&thread_id1, NULL, some_function, &value1);
pthread_create(&thread_id2, NULL, test_static_variable_with_threads, &value2);
pthread_create(&thread_id3, NULL, test_static_variable_with_threads, &value3);
pthread_join(thread_id1, &exit_status1);
pthread_join(thread_id2, &exit_status2);
pthread_join(thread_id3, &exit_status3);
input = getchar();
//scanf("%d",&array_length);
//vla_test(array_length);
return 0;
}
void vla_test(unsigned int length)
{
int vla_array[length];
int i;
for (i=0; i<length; i++)
{
vla_array[i] = i;
}
for (i=0; i<length; i++)
{
printf("%d\n", vla_array[i]);
}
}
void *some_function(void *arg)
{
int i;
i = *(int*)arg;
int j;
pthread_t tid = pthread_self();
for (j=0; j<10; j++)
{
printf("%lu: %d \n", tid, i);
usleep(500);
i--;
}
return NULL;
}
void *test_static_variable_with_threads(void *arg)
{
static int i = 0;
pthread_t tid = pthread_self();
pthread_mutex_t mutex;
pthread_mutex_init(&mutex, NULL);
while (i<20)
{
pthread_mutex_lock(&mutex);
i++;
printf("value of the static variable in thread %lu: %d\n", tid, i);
pthread_mutex_unlock(&mutex);
usleep(100);
}
pthread_mutex_destroy(&mutex);
return NULL;
}