Created
April 23, 2020 21:34
-
-
Save jimblandy/a9b339b046d2abb979f1e901d69ca84a to your computer and use it in GitHub Desktop.
Program to create N boring threads that do nothing, for measuring minimal per-thread memory use
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <pthread.h> | |
#include <stdatomic.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#define NUM_THREADS 500 | |
#define BYTES_PER_THREAD 1024 * 1024 // 1 MB | |
void *child_main(void *num_threads_ready) { | |
/* | |
char *data = malloc(BYTES_PER_THREAD); | |
if (data == NULL) { | |
fputs("failed to allocate memory", stderr); | |
exit(1); | |
} | |
for (int i = 0; i < BYTES_PER_THREAD; i++) { | |
data[i] = 0; | |
} | |
*/ | |
atomic_fetch_add((atomic_uint *)num_threads_ready, 1); | |
pause(); | |
return 0; | |
} | |
int main() { | |
atomic_uint num_threads_ready; | |
atomic_init(&num_threads_ready, 0); | |
pthread_t threads[NUM_THREADS]; | |
fputs("starting threads... ", stderr); | |
for (int i = 0; i < NUM_THREADS; i++) { | |
if (pthread_create(&threads[i], NULL, child_main, &num_threads_ready)) { | |
fputs("failed to create thread\n", stderr); | |
exit(1); | |
} | |
} | |
while (atomic_load(&num_threads_ready) < NUM_THREADS) { | |
sleep(1); | |
} | |
fputs("done\npress enter to exit", stderr); | |
getchar(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment