pthreads/{hello_w/hello_w.c → hello_iw_pri/src/hello_iw_pri.c} RENAMED
@@ -1,86 +1,92 @@
1
  // Copyright 2021 Jeisson Hidalgo <jeisson.hidalgo@ucr.ac.cr> CC-BY 4.0
2
 
3
  #include <assert.h>
4
  #include <inttypes.h>
5
  #include <pthread.h>
6
  #include <stdint.h>
7
  #include <stdio.h>
8
  #include <stdlib.h>
9
  #include <unistd.h>
10
 
 
 
 
 
 
 
 
11
  /**
12
  * @brief ...
13
  */
14
  void* greet(void* data);
15
  int create_threads(uint64_t thread_count);
16
 
17
  // procedure main(argc, argv[])
18
  int main(int argc, char* argv[]) {
19
- #if 0
20
- for (int index = 0; index < argc; ++index) {
21
- printf("argv[%d] = '%s'\n", index, argv[index]);
22
- }
23
- return 0;
24
- #endif
25
-
26
  int error = EXIT_SUCCESS;
27
  // create thread_count as result of converting argv[1] to integer
28
  // thread_count := integer(argv[1])
29
  uint64_t thread_count = sysconf(_SC_NPROCESSORS_ONLN);
30
  if (argc == 2) {
31
  if (sscanf(argv[1], "%" SCNu64, &thread_count) == 1) {
32
  } else {
33
  fprintf(stderr, "Error: invalid thread count\n");
34
  return 11;
35
  }
36
  }
37
 
38
  error = create_threads(thread_count);
39
  return error;
40
  } // end procedure
41
 
42
 
43
  int create_threads(uint64_t thread_count) {
44
  int error = EXIT_SUCCESS;
45
  // for thread_number := 0 to thread_count do
46
  pthread_t* threads = (pthread_t*) malloc(thread_count * sizeof(pthread_t));
47
- if (threads) {
 
 
48
  for (uint64_t thread_number = 0; thread_number < thread_count
49
  ; ++thread_number) {
 
 
50
  // create_thread(greet, thread_number)
51
  error = pthread_create(&threads[thread_number], /*attr*/ NULL, greet
52
- , /*arg*/ (void*) thread_number);
53
  if (error == EXIT_SUCCESS) {
54
  } else {
55
  fprintf(stderr, "Error: could not create secondary thread\n");
56
  error = 21;
57
  break;
58
  }
59
  }
60
 
61
  // print "Hello from main thread"
62
  printf("Hello from main thread\n");
63
 
64
  for (uint64_t thread_number = 0; thread_number < thread_count
65
  ; ++thread_number) {
66
  pthread_join(threads[thread_number], /*value_ptr*/ NULL);
67
  }
68
 
 
69
  free(threads);
70
  } else {
71
  fprintf(stderr, "Error: could not allocate %" PRIu64 " threads\n"
72
  , thread_count);
73
  error = 22;
74
  }
75
 
76
  return error;
77
  }
78
 
79
  // procedure greet:
80
  void* greet(void* data) {
81
  // assert(data);
82
- const uint64_t rank = (uint64_t) data;
83
  // print "Hello from secondary thread"
84
- printf("Hello from secondary thread %" PRIu64 "\n", rank);
 
85
  return NULL;
86
  } // end procedure
1
  // Copyright 2021 Jeisson Hidalgo <jeisson.hidalgo@ucr.ac.cr> CC-BY 4.0
2
 
3
  #include <assert.h>
4
  #include <inttypes.h>
5
  #include <pthread.h>
6
  #include <stdint.h>
7
  #include <stdio.h>
8
  #include <stdlib.h>
9
  #include <unistd.h>
10
 
11
+ // thread_private_data_t
12
+ typedef struct private_data {
13
+ uint64_t thread_number; // rank
14
+ uint64_t thread_count;
15
+ struct private_data* next;
16
+ } private_data_t;
17
+
18
  /**
19
  * @brief ...
20
  */
21
  void* greet(void* data);
22
  int create_threads(uint64_t thread_count);
23
 
24
  // procedure main(argc, argv[])
25
  int main(int argc, char* argv[]) {
 
 
 
 
 
 
 
26
  int error = EXIT_SUCCESS;
27
  // create thread_count as result of converting argv[1] to integer
28
  // thread_count := integer(argv[1])
29
  uint64_t thread_count = sysconf(_SC_NPROCESSORS_ONLN);
30
  if (argc == 2) {
31
  if (sscanf(argv[1], "%" SCNu64, &thread_count) == 1) {
32
  } else {
33
  fprintf(stderr, "Error: invalid thread count\n");
34
  return 11;
35
  }
36
  }
37
 
38
  error = create_threads(thread_count);
39
  return error;
40
  } // end procedure
41
 
42
 
43
  int create_threads(uint64_t thread_count) {
44
  int error = EXIT_SUCCESS;
45
  // for thread_number := 0 to thread_count do
46
  pthread_t* threads = (pthread_t*) malloc(thread_count * sizeof(pthread_t));
47
+ private_data_t* private_data = (private_data_t*)
48
+ calloc(thread_count, sizeof(private_data_t));
49
+ if (threads && private_data) {
50
  for (uint64_t thread_number = 0; thread_number < thread_count
51
  ; ++thread_number) {
52
+ private_data[thread_number].thread_number = thread_number;
53
+ private_data[thread_number].thread_count = thread_count;
54
  // create_thread(greet, thread_number)
55
  error = pthread_create(&threads[thread_number], /*attr*/ NULL, greet
56
+ , /*arg*/ &private_data[thread_number]);
57
  if (error == EXIT_SUCCESS) {
58
  } else {
59
  fprintf(stderr, "Error: could not create secondary thread\n");
60
  error = 21;
61
  break;
62
  }
63
  }
64
 
65
  // print "Hello from main thread"
66
  printf("Hello from main thread\n");
67
 
68
  for (uint64_t thread_number = 0; thread_number < thread_count
69
  ; ++thread_number) {
70
  pthread_join(threads[thread_number], /*value_ptr*/ NULL);
71
  }
72
 
73
+ free(private_data);
74
  free(threads);
75
  } else {
76
  fprintf(stderr, "Error: could not allocate %" PRIu64 " threads\n"
77
  , thread_count);
78
  error = 22;
79
  }
80
 
81
  return error;
82
  }
83
 
84
  // procedure greet:
85
  void* greet(void* data) {
86
  // assert(data);
87
+ private_data_t* private_data = (private_data_t*) data;
88
  // print "Hello from secondary thread"
89
+ printf("Hello from secondary thread %" PRIu64 " of %" PRIu64 "\n"
90
+ , (*private_data).thread_number, private_data->thread_count);
91
  return NULL;
92
  } // end procedure