-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08-thread.c
38 lines (32 loc) · 834 Bytes
/
08-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
// Run Using "gcc -pthread 08_thread.c && ./a.out"
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void* thread_function(void* vargp) {
int* i = (int*)vargp;
printf("Executing: thread_function(%d)\n", *i);
return NULL;
}
int main() {
pthread_t tid[5];
// Shared Variable
printf("Shared Variables:\n");
for (int i=0; i<5; i++) {
pthread_create(&tid[i], NULL, thread_function, (void*)&i);
}
for (int i=0; i<5; i++) {
pthread_join(tid[i], NULL);
}
// Unsahared Variable
printf("Unshared Variables:\n");
int arr[5];
for (int i=0; i<5; i++) {
arr[i] = i;
pthread_create(&tid[i], NULL, thread_function, (void*)&arr[i]);
}
for (int i=0; i<5; i++) {
pthread_join(tid[i], NULL);
}
return 0;
}