pthread_self: allow multiple waves

Original motivation: determine if gem5's join releases CPU cores.
It does.
This commit is contained in:
Ciro Santilli 六四事件 法轮功
2019-11-04 00:00:00 +00:00
parent a06672a20d
commit 425d3a5713

View File

@@ -57,7 +57,7 @@ void* main_thread(void *arg) {
int main(int argc, char**argv) { int main(int argc, char**argv) {
pthread_t *threads; pthread_t *threads;
unsigned int nthreads, i, *thread_args; unsigned int nthreads, nwaves, i, *thread_args, wave;
int rc; int rc;
/* CLI arguments. */ /* CLI arguments. */
@@ -66,41 +66,48 @@ int main(int argc, char**argv) {
} else { } else {
nthreads = 1; nthreads = 1;
} }
if (argc > 2) {
nwaves = strtoll(argv[2], NULL, 0);
} else {
nwaves = 1;
}
threads = malloc(nthreads * sizeof(*threads)); threads = malloc(nthreads * sizeof(*threads));
thread_args = malloc(nthreads * sizeof(*thread_args)); thread_args = malloc(nthreads * sizeof(*thread_args));
/* main thread for comparison. */ for (wave = 0; wave < nwaves; wave++) {
printf( /* main thread for comparison. */
"tid, getpid(), pthread_self() = " printf(
"main, %ju, %ju\n", "tid, getpid(), pthread_self() = "
(uintmax_t)getpid(), "main, %ju, %ju\n",
(uintmax_t)pthread_self() (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]
); );
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 */ /* Create all threads */
for (i = 0; i < nthreads; ++i) { for (i = 0; i < nthreads; ++i) {
rc = pthread_join(threads[i], NULL); thread_args[i] = i;
if (rc != 0) { rc = pthread_create(
printf("%s\n", strerror(rc)); &threads[i],
exit(EXIT_FAILURE); 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);
}
} }
} }