@@ -1,39 +1,83 @@
|
|
1 |
/*
|
2 |
* Copyright 2021 Jeisson Hidalgo-Cespedes - Universidad de Costa Rica
|
3 |
* Creates a secondary thread that greets in the standard output
|
4 |
*/
|
5 |
|
|
|
6 |
#include <pthread.h>
|
|
|
7 |
#include <stdio.h>
|
|
|
8 |
|
9 |
-
void* run(void* data)
|
10 |
-
|
11 |
-
pthread_mutex_lock(stdout_mutex);
|
12 |
-
printf("Hello from secondary thread, mutex at %p\n", data);
|
13 |
-
pthread_mutex_unlock(stdout_mutex);
|
14 |
|
15 |
-
|
16 |
-
}
|
17 |
-
|
18 |
-
int main(void) {
|
19 |
-
pthread_t thread;
|
20 |
pthread_mutex_t stdout_mutex;
|
|
|
|
|
21 |
int error = pthread_mutex_init(&stdout_mutex, /*attr*/ NULL);
|
|
|
22 |
if (error == 0) {
|
23 |
-
|
24 |
-
if (
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
pthread_mutex_lock(&stdout_mutex);
|
26 |
printf("Hello from main thread\n");
|
27 |
pthread_mutex_unlock(&stdout_mutex);
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
}
|
32 |
-
|
33 |
} else {
|
34 |
-
fprintf(stderr, "error: could not
|
|
|
|
|
35 |
}
|
36 |
-
|
|
|
37 |
}
|
38 |
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
/*
|
2 |
* Copyright 2021 Jeisson Hidalgo-Cespedes - Universidad de Costa Rica
|
3 |
* Creates a secondary thread that greets in the standard output
|
4 |
*/
|
5 |
|
6 |
+
#include <inttypes.h>
|
7 |
#include <pthread.h>
|
8 |
+
#include <stdint.h>
|
9 |
#include <stdio.h>
|
10 |
+
#include <stdlib.h>
|
11 |
|
12 |
+
void* run(void* data);
|
13 |
+
int create_threads(size_t thread_count);
|
|
|
|
|
|
|
14 |
|
15 |
+
// TODO(you): do not use global variables
|
|
|
|
|
|
|
|
|
16 |
pthread_mutex_t stdout_mutex;
|
17 |
+
|
18 |
+
int main(int argc, char* argv[]) {
|
19 |
int error = pthread_mutex_init(&stdout_mutex, /*attr*/ NULL);
|
20 |
+
|
21 |
if (error == 0) {
|
22 |
+
size_t thread_count = 1;
|
23 |
+
if (argc >= 2) {
|
24 |
+
thread_count = strtoull(argv[1], NULL, 10);
|
25 |
+
}
|
26 |
+
|
27 |
+
error = create_threads(thread_count);
|
28 |
+
|
29 |
+
pthread_mutex_destroy(&stdout_mutex);
|
30 |
+
} else {
|
31 |
+
fprintf(stderr, "error: could not init mutex\n");
|
32 |
+
error = 11;
|
33 |
+
}
|
34 |
+
|
35 |
+
return error;
|
36 |
+
}
|
37 |
+
|
38 |
+
int create_threads(size_t thread_count) {
|
39 |
+
pthread_t* threads = (pthread_t*) malloc(thread_count * sizeof(pthread_t));
|
40 |
+
int error = 0;
|
41 |
+
|
42 |
+
if (threads) {
|
43 |
+
for (size_t index = 0; index < thread_count; ++index) {
|
44 |
+
/*&stdout_mutex*/
|
45 |
+
error = pthread_create(&threads[index], NULL, run, (void*)index);
|
46 |
+
|
47 |
+
if (error) {
|
48 |
+
fprintf(stderr, "error: could not create thread %zu\n", index);
|
49 |
+
error = 21;
|
50 |
+
}
|
51 |
+
}
|
52 |
+
|
53 |
pthread_mutex_lock(&stdout_mutex);
|
54 |
printf("Hello from main thread\n");
|
55 |
pthread_mutex_unlock(&stdout_mutex);
|
56 |
+
|
57 |
+
for (size_t index = 0; index < thread_count; ++index) {
|
58 |
+
pthread_join(threads[index], NULL);
|
59 |
}
|
60 |
+
free(threads);
|
61 |
} else {
|
62 |
+
fprintf(stderr, "error: could not allocate memory for %zu threads\n"
|
63 |
+
, thread_count);
|
64 |
+
error = 22;
|
65 |
}
|
66 |
+
|
67 |
+
return error;
|
68 |
}
|
69 |
|
70 |
+
void* run(void* data) {
|
71 |
+
// pthread_mutex_t* stdout_mutex = (pthread_mutex_t*)data;
|
72 |
+
pthread_mutex_lock(&stdout_mutex);
|
73 |
+
printf("Hello from secondary thread %zu\n", (size_t)data);
|
74 |
+
pthread_mutex_unlock(&stdout_mutex);
|
75 |
+
|
76 |
+
return NULL;
|
77 |
+
}
|
78 |
+
|
79 |
+
#if 0
|
80 |
+
for (int index = 0; index < argc; ++index) {
|
81 |
+
printf("argv[%d] = {%s}\n", index, argv[index]);
|
82 |
+
}
|
83 |
+
#endif
|