|
@@ -1,60 +1,71 @@
|
|
| 1 |
// Copyright 2021 Jeisson Hidalgo-Cespedes <jeisson.hidalgo@ucr.ac.cr> CC-BY-4
|
| 2 |
// Creates an arbitrary amount of threads that greet in stdout
|
| 3 |
|
| 4 |
#include <errno.h>
|
| 5 |
#include <pthread.h>
|
| 6 |
#include <stdio.h>
|
| 7 |
#include <stdlib.h>
|
| 8 |
#include <unistd.h>
|
| 9 |
|
| 10 |
int create_threads(size_t thread_count);
|
| 11 |
void* run(void* data);
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
int main(int argc, char* argv[]) {
|
| 14 |
size_t thread_count = sysconf(_SC_NPROCESSORS_ONLN);
|
| 15 |
if (argc == 2) {
|
| 16 |
if (sscanf(argv[1], "%zu", &thread_count) != 1 || errno) {
|
| 17 |
fprintf(stderr, "error: invalid thread count\n");
|
| 18 |
return EXIT_FAILURE;
|
| 19 |
}
|
| 20 |
}
|
| 21 |
|
| 22 |
int error = create_threads(thread_count);
|
| 23 |
return error;
|
| 24 |
}
|
| 25 |
|
| 26 |
int create_threads(size_t thread_count) {
|
| 27 |
int error = EXIT_SUCCESS;
|
| 28 |
pthread_t* threads = (pthread_t*) calloc(thread_count, sizeof(pthread_t));
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
for (size_t index = 0; index < thread_count; ++index) {
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
} else {
|
| 34 |
fprintf(stderr, "Could not create secondary thread %zu\n", index);
|
| 35 |
error = 21;
|
| 36 |
break;
|
| 37 |
}
|
| 38 |
}
|
| 39 |
|
| 40 |
printf("Hello from main thread\n");
|
| 41 |
|
| 42 |
for (size_t index = 0; index < thread_count; ++index) {
|
| 43 |
pthread_join(threads[index], /*value_ptr*/ NULL);
|
| 44 |
}
|
| 45 |
free(threads);
|
|
|
|
| 46 |
} else {
|
| 47 |
fprintf(stderr, "Could not allocate memory for %zu threads\n"
|
| 48 |
, thread_count);
|
| 49 |
error = 22;
|
| 50 |
}
|
| 51 |
|
| 52 |
return error;
|
| 53 |
}
|
| 54 |
|
| 55 |
void* run(void* data) {
|
| 56 |
-
const
|
| 57 |
-
printf("Hello from secondary thread %zu\n", thread_number
|
|
|
|
| 58 |
|
| 59 |
return NULL;
|
| 60 |
}
|
| 1 |
// Copyright 2021 Jeisson Hidalgo-Cespedes <jeisson.hidalgo@ucr.ac.cr> CC-BY-4
|
| 2 |
// Creates an arbitrary amount of threads that greet in stdout
|
| 3 |
|
| 4 |
#include <errno.h>
|
| 5 |
#include <pthread.h>
|
| 6 |
#include <stdio.h>
|
| 7 |
#include <stdlib.h>
|
| 8 |
#include <unistd.h>
|
| 9 |
|
| 10 |
int create_threads(size_t thread_count);
|
| 11 |
void* run(void* data);
|
| 12 |
|
| 13 |
+
typedef struct {
|
| 14 |
+
size_t thread_number;
|
| 15 |
+
size_t thread_count;
|
| 16 |
+
} private_data_t;
|
| 17 |
+
|
| 18 |
int main(int argc, char* argv[]) {
|
| 19 |
size_t thread_count = sysconf(_SC_NPROCESSORS_ONLN);
|
| 20 |
if (argc == 2) {
|
| 21 |
if (sscanf(argv[1], "%zu", &thread_count) != 1 || errno) {
|
| 22 |
fprintf(stderr, "error: invalid thread count\n");
|
| 23 |
return EXIT_FAILURE;
|
| 24 |
}
|
| 25 |
}
|
| 26 |
|
| 27 |
int error = create_threads(thread_count);
|
| 28 |
return error;
|
| 29 |
}
|
| 30 |
|
| 31 |
int create_threads(size_t thread_count) {
|
| 32 |
int error = EXIT_SUCCESS;
|
| 33 |
pthread_t* threads = (pthread_t*) calloc(thread_count, sizeof(pthread_t));
|
| 34 |
+
private_data_t* private_data = (private_data_t*)
|
| 35 |
+
calloc(thread_count, sizeof(private_data_t));
|
| 36 |
+
if (threads && private_data) {
|
| 37 |
for (size_t index = 0; index < thread_count; ++index) {
|
| 38 |
+
private_data[index].thread_number = index;
|
| 39 |
+
private_data[index].thread_count = thread_count;
|
| 40 |
+
if (pthread_create(&threads[index], /*attr*/ NULL, run
|
| 41 |
+
, &private_data[index]) == EXIT_SUCCESS) {
|
| 42 |
} else {
|
| 43 |
fprintf(stderr, "Could not create secondary thread %zu\n", index);
|
| 44 |
error = 21;
|
| 45 |
break;
|
| 46 |
}
|
| 47 |
}
|
| 48 |
|
| 49 |
printf("Hello from main thread\n");
|
| 50 |
|
| 51 |
for (size_t index = 0; index < thread_count; ++index) {
|
| 52 |
pthread_join(threads[index], /*value_ptr*/ NULL);
|
| 53 |
}
|
| 54 |
free(threads);
|
| 55 |
+
free(private_data);
|
| 56 |
} else {
|
| 57 |
fprintf(stderr, "Could not allocate memory for %zu threads\n"
|
| 58 |
, thread_count);
|
| 59 |
error = 22;
|
| 60 |
}
|
| 61 |
|
| 62 |
return error;
|
| 63 |
}
|
| 64 |
|
| 65 |
void* run(void* data) {
|
| 66 |
+
const private_data_t* private_data = (private_data_t*)data;
|
| 67 |
+
printf("Hello from secondary thread %zu of %zu\n", private_data->thread_number
|
| 68 |
+
, private_data->thread_count);
|
| 69 |
|
| 70 |
return NULL;
|
| 71 |
}
|