diff --git a/userland/posix/pthread_self.c b/userland/posix/pthread_self.c index ed31227..0c1d5ee 100644 --- a/userland/posix/pthread_self.c +++ b/userland/posix/pthread_self.c @@ -57,7 +57,7 @@ void* main_thread(void *arg) { int main(int argc, char**argv) { pthread_t *threads; - unsigned int nthreads, i, *thread_args; + unsigned int nthreads, nwaves, i, *thread_args, wave; int rc; /* CLI arguments. */ @@ -66,41 +66,48 @@ int main(int argc, char**argv) { } else { nthreads = 1; } + if (argc > 2) { + nwaves = strtoll(argv[2], NULL, 0); + } else { + nwaves = 1; + } threads = malloc(nthreads * sizeof(*threads)); thread_args = malloc(nthreads * sizeof(*thread_args)); - /* main thread for comparison. */ - printf( - "tid, getpid(), pthread_self() = " - "main, %ju, %ju\n", - (uintmax_t)getpid(), - (uintmax_t)pthread_self() - ); - - /* Create all threads */ - for (i = 0; i < nthreads; ++i) { - thread_args[i] = i; - rc = pthread_create( - &threads[i], - NULL, - main_thread, - (void*)&thread_args[i] + for (wave = 0; wave < nwaves; wave++) { + /* main thread for comparison. */ + printf( + "tid, getpid(), pthread_self() = " + "main, %ju, %ju\n", + (uintmax_t)getpid(), + (uintmax_t)pthread_self() ); - if (rc != 0) { - errno = rc; - perror("pthread_create"); - exit(EXIT_FAILURE); - } - assert(rc == 0); - printf("%d tid: %ju\n", i, (uintmax_t)threads[i]); - } - /* Wait for all threads to complete */ - for (i = 0; i < nthreads; ++i) { - rc = pthread_join(threads[i], NULL); - if (rc != 0) { - printf("%s\n", strerror(rc)); - exit(EXIT_FAILURE); + /* Create all threads */ + for (i = 0; i < nthreads; ++i) { + thread_args[i] = i; + rc = pthread_create( + &threads[i], + NULL, + main_thread, + (void*)&thread_args[i] + ); + if (rc != 0) { + errno = rc; + perror("pthread_create"); + exit(EXIT_FAILURE); + } + assert(rc == 0); + printf("%d tid: %ju\n", i, (uintmax_t)threads[i]); + } + + /* Wait for all threads to complete */ + for (i = 0; i < nthreads; ++i) { + rc = pthread_join(threads[i], NULL); + if (rc != 0) { + printf("%s\n", strerror(rc)); + exit(EXIT_FAILURE); + } } }