-
-
Save mflatt/eda1c765c20393b97d6ee94e4e6ce517 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <stdio.h> | |
| #include <pthread.h> | |
| #define THREADED 1 | |
| #define PRINT_IN_PIECES 0 | |
| #define N 4 | |
| typedef struct { | |
| int id; | |
| FILE *f; | |
| } data_t; | |
| void *go(void *_data) { | |
| data_t *data = _data; | |
| for (int i = 0; i < 100000; i++) { | |
| if (PRINT_IN_PIECES) { | |
| fprintf(data->f, "\""); | |
| fprintf(data->f, "Thread %d: %d\n", data->id, i); | |
| fprintf(data->f, "\""); | |
| fprintf(data->f, "\n"); | |
| } else | |
| fprintf(data->f, "Thread %d: %d\n", data->id, i); | |
| } | |
| return NULL; | |
| } | |
| void *nothing(void *no_data) { | |
| return NULL; | |
| } | |
| int main() { | |
| FILE *f; | |
| pthread_t th[N]; | |
| data_t data[N]; | |
| f = fopen("/tmp/randomness.txt", "w"); | |
| for (int i = 0; i < N; i++) { | |
| data[i].id = i; | |
| data[i].f = f; | |
| if (THREADED) | |
| pthread_create(&th[i], NULL, go, &data[i]); | |
| else { | |
| pthread_create(&th[i], NULL, nothing, NULL); | |
| go(&data[i]); | |
| } | |
| } | |
| for (int i = 0; i < N; i++) { | |
| pthread_join(th[i], NULL); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment