| 
         @@ -1,27 +1,60 @@ 
     | 
|
| 1 | 
         
             
            // Copyright 2021 Jeisson Hidalgo-Cespedes <jeisson.hidalgo@ucr.ac.cr> CC-BY-4
         
     | 
| 2 | 
         
            -
            // Creates  
     | 
| 3 | 
         | 
| 
         | 
|
| 4 | 
         
             
            #include <pthread.h>
         
     | 
| 5 | 
         
             
            #include <stdio.h>
         
     | 
| 6 | 
         
             
            #include <stdlib.h>
         
     | 
| 
         | 
|
| 7 | 
         | 
| 
         | 
|
| 8 | 
         
             
            void* run(void* data);
         
     | 
| 9 | 
         | 
| 10 | 
         
            -
            int main( 
     | 
| 11 | 
         
            -
               
     | 
| 12 | 
         
            -
              if ( 
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 13 | 
         
             
                printf("Hello from main thread\n");
         
     | 
| 14 | 
         
            -
             
     | 
| 15 | 
         
            -
                 
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 16 | 
         
             
              } else {
         
     | 
| 17 | 
         
            -
                fprintf(stderr, "Could not  
     | 
| 18 | 
         
            -
             
     | 
| 
         | 
|
| 19 | 
         
             
              }
         
     | 
| 
         | 
|
| 
         | 
|
| 20 | 
         
             
            }
         
     | 
| 21 | 
         | 
| 22 | 
         
             
            void* run(void* data) {
         
     | 
| 23 | 
         
            -
               
     | 
| 24 | 
         
            -
              printf(" 
     | 
| 25 | 
         | 
| 26 | 
         
             
              return NULL;
         
     | 
| 27 | 
         
             
            }
         
     | 
| 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 | 
         
            +
              if (threads) {
         
     | 
| 30 | 
         
            +
                for (size_t index = 0; index < thread_count; ++index) {
         
     | 
| 31 | 
         
            +
                  if (pthread_create(&threads[index], /*attr*/ NULL, run, (void*)index)
         
     | 
| 32 | 
         
            +
                      == EXIT_SUCCESS) {
         
     | 
| 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 size_t thread_number = (size_t)data;
         
     | 
| 57 | 
         
            +
              printf("Hello from secondary thread %zu\n", thread_number);
         
     | 
| 58 | 
         | 
| 59 | 
         
             
              return NULL;
         
     | 
| 60 | 
         
             
            }
         
     |