-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathargs.c
More file actions
46 lines (33 loc) · 960 Bytes
/
Copy pathargs.c
File metadata and controls
46 lines (33 loc) · 960 Bytes
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
// 2026 S1C COMP2017 Week 10 Tutorial B
// Tutor: Hao Ren (hao.ren@sydney.edu.au)
// Hello from threads with arguments.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NTHREADS 10
static void *routine(void *args) {
int id = *(int *)args;
printf("Hello From Thread %d!\n", id);
return NULL;
}
int main(void) {
pthread_t threads[NTHREADS];
int ids[NTHREADS];
for (int i = 0; i < NTHREADS; i++) {
ids[i] = i;
int err = pthread_create(&threads[i], NULL, routine, &ids[i]);
if (err != 0) {
fprintf(stderr, "pthread_create: %s\n", strerror(err));
return EXIT_FAILURE;
}
}
for (int i = 0; i < NTHREADS; i++) {
int err = pthread_join(threads[i], NULL);
if (err != 0) {
fprintf(stderr, "pthread_join: %s\n", strerror(err));
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}