|
@@ -1,24 +1,68 @@
|
|
| 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 |
size_t thread_id = (size_t)data;
|
| 12 |
-
printf("
|
| 13 |
return NULL;
|
| 14 |
}
|
| 15 |
|
| 16 |
-
int main(
|
| 17 |
{
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
printf("Hello from main thread\n");
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
return 0;
|
| 24 |
}
|
| 1 |
#include <pthread.h>
|
| 2 |
#include <stdio.h>
|
| 3 |
+
#include <stdlib.h>
|
| 4 |
+
#include <time.h>
|
| 5 |
+
#include <unistd.h>
|
| 6 |
|
| 7 |
/*
|
| 8 |
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
|
| 9 |
void* (*start_routine)(void*), void *arg);
|
| 10 |
*/
|
| 11 |
|
| 12 |
void* run(void* data)
|
| 13 |
{
|
| 14 |
size_t thread_id = (size_t)data;
|
| 15 |
+
printf("Hello from thread %zu\n", thread_id);
|
| 16 |
return NULL;
|
| 17 |
}
|
| 18 |
|
| 19 |
+
int main(int argc, char* argv[])
|
| 20 |
{
|
| 21 |
+
#if defined(PRINT_ARGS)
|
| 22 |
+
for ( int index = 0; index < argc; ++index )
|
| 23 |
+
printf("%d[%s]\n", index, argv[index]);
|
| 24 |
+
#endif
|
| 25 |
+
|
| 26 |
+
size_t thread_count = sysconf(_SC_NPROCESSORS_ONLN);
|
| 27 |
+
if ( argc >= 2 )
|
| 28 |
+
{
|
| 29 |
+
// thread_count = strtoull(argv[1], NULL, 10);
|
| 30 |
+
if ( sscanf(argv[1], "%zu", &thread_count) != 1 || thread_count == 0 )
|
| 31 |
+
return (void)fprintf(stderr, "hello_w: error: invalid thread count: %s\n", argv[1]), 1;
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
pthread_t* threads = (pthread_t*)malloc(thread_count * sizeof(pthread_t));
|
| 35 |
+
if ( threads == NULL )
|
| 36 |
+
return (void)fprintf(stderr, "hello_w: error: could not allocate memory for: %zu threads\n", thread_count), 2;
|
| 37 |
+
|
| 38 |
+
//int clock_gettime(clockid_t clk_id, struct timespec *tp);
|
| 39 |
+
struct timespec start_time;
|
| 40 |
+
clock_gettime(CLOCK_MONOTONIC, &start_time);
|
| 41 |
+
|
| 42 |
+
for ( size_t index = 0; index < thread_count; ++index )
|
| 43 |
+
pthread_create(&threads[index], NULL, run, (void*)index);
|
| 44 |
+
|
| 45 |
printf("Hello from main thread\n");
|
| 46 |
+
|
| 47 |
+
for ( size_t index = 0; index < thread_count; ++index )
|
| 48 |
+
pthread_join(threads[index], NULL);
|
| 49 |
+
|
| 50 |
+
#if 0
|
| 51 |
+
struct timespec {
|
| 52 |
+
time_t tv_sec; /* seconds */
|
| 53 |
+
long tv_nsec; /* nanoseconds */
|
| 54 |
+
};
|
| 55 |
+
#endif
|
| 56 |
+
|
| 57 |
+
struct timespec finish_time;
|
| 58 |
+
clock_gettime(CLOCK_MONOTONIC, &finish_time);
|
| 59 |
+
|
| 60 |
+
double seconds = finish_time.tv_sec - start_time.tv_sec
|
| 61 |
+
+ (finish_time.tv_nsec - start_time.tv_nsec) * 1e-9;
|
| 62 |
+
|
| 63 |
+
printf("Thread creation and join time: %.9lfs\n", seconds);
|
| 64 |
+
|
| 65 |
+
free(threads);
|
| 66 |
|
| 67 |
return 0;
|
| 68 |
}
|