pthreads/{hello/hello.c → hello_w/hello_w.c} RENAMED
@@ -1,25 +1,37 @@
1
  #include <pthread.h>
2
  #include <stdio.h>
3
-
4
- /*
5
- int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
6
- void *(*start_routine)(void*), void *arg);
7
- */
8
 
9
  void* run(void* data)
10
  {
11
- //(void)data;
12
  size_t thread_num = (size_t)data;
 
 
13
  printf("Hello world from secondary thread %zu\n", thread_num);
14
  return NULL;
15
  }
16
 
17
- int main(void)
18
  {
19
- pthread_t thread;
20
- pthread_create(&thread, NULL, run, (void*)13);
 
 
 
 
 
 
 
 
 
 
 
21
  printf("Hello world from main thread\n");
22
- pthread_join(thread, NULL);
23
 
 
 
 
 
24
  return 0;
25
  }
1
  #include <pthread.h>
2
  #include <stdio.h>
3
+ #include <stdlib.h>
4
+ #include <unistd.h>
 
 
 
5
 
6
  void* run(void* data)
7
  {
 
8
  size_t thread_num = (size_t)data;
9
+ // if ( thread_num < 10 )
10
+ // sleep(1);
11
  printf("Hello world from secondary thread %zu\n", thread_num);
12
  return NULL;
13
  }
14
 
15
+ int main(int argc, char* argv[])
16
  {
17
+ // for ( int index = 0; index < argc; ++index )
18
+ // fprintf(stderr, "%d[%s]\n", index, argv[index]);
19
+
20
+ size_t thread_count = sysconf(_SC_NPROCESSORS_ONLN);
21
+ if ( argc >= 2 )
22
+ thread_count = strtoull(argv[1], NULL, 10);
23
+
24
+ //pthread_t thread[thread_count];
25
+ pthread_t* threads = (pthread_t*) malloc(thread_count * sizeof(pthread_t));
26
+
27
+ for ( size_t index = 0; index < thread_count; ++index )
28
+ pthread_create(&threads[index], NULL, run, (void*)index);
29
+
30
  printf("Hello world from main thread\n");
 
31
 
32
+ for ( size_t index = 0; index < thread_count; ++index )
33
+ pthread_join(threads[index], NULL);
34
+
35
+ free(threads);
36
  return 0;
37
  }