-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreads.c
40 lines (24 loc) · 802 Bytes
/
threads.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
#include <stdio.h>
#include <pthread.h>
void *thread_function(void *arg) {
printf("Hello from the thread\n");
sleep(30);
return NULL;
}
int main() {
// Multiple threads
pthread_t thread_id1, thread_id2, thread_id3;
pthread_create(&thread_id1, NULL, thread_function, NULL);
pthread_create(&thread_id2, NULL, thread_function, NULL);
pthread_create(&thread_id2, NULL, thread_function, NULL);
printf("Hello from the main thread\n");
pthread_join(thread_id1, NULL);
pthread_join(thread_id2, NULL);
pthread_join(thread_id3, NULL);
// Single thread
// pthread_t thread_id;
// pthread_create(&thread_id, NULL, thread_function, NULL);
// printf("Hello from the main thread\n");
// pthread_join(thread_id, NULL);
return 0;
}