Last active
December 27, 2019 07:23
-
-
Save WaryWolf/ea7d524f96725d823aae5d96a3727442 to your computer and use it in GitHub Desktop.
pthread testing code
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
$ ./whyc | |
starting... | |
jobname.2019-12-27_17.23.33_01 (seq = 05, lseq = 01) | |
jobname.2019-12-27_17.23.33_05 (seq = 07, lseq = 05) | |
jobname.2019-12-27_17.23.33_03 (seq = 07, lseq = 03) | |
jobname.2019-12-27_17.23.33_08 (seq = 11, lseq = 08) | |
jobname.2019-12-27_17.23.33_06 (seq = 11, lseq = 06) | |
jobname.2019-12-27_17.23.33_02 (seq = 11, lseq = 02) | |
jobname.2019-12-27_17.23.33_07 (seq = 12, lseq = 07) | |
jobname.2019-12-27_17.23.33_04 (seq = 13, lseq = 04) | |
jobname.2019-12-27_17.23.33_09 (seq = 13, lseq = 09) | |
jobname.2019-12-27_17.23.33_10 (seq = 14, lseq = 10) | |
jobname.2019-12-27_17.23.33_14 (seq = 14, lseq = 14) | |
jobname.2019-12-27_17.23.33_12 (seq = 14, lseq = 12) | |
jobname.2019-12-27_17.23.33_13 (seq = 14, lseq = 13) | |
jobname.2019-12-27_17.23.33_15 (seq = 15, lseq = 15) | |
jobname.2019-12-27_17.23.33_11 (seq = 16, lseq = 11) | |
jobname.2019-12-27_17.23.33_17 (seq = 17, lseq = 17) | |
jobname.2019-12-27_17.23.33_16 (seq = 17, lseq = 16) | |
jobname.2019-12-27_17.23.33_18 (seq = 18, lseq = 18) | |
jobname.2019-12-27_17.23.33_19 (seq = 19, lseq = 19) | |
threads dispatched. waiting... | |
jobname.2019-12-27_17.23.33_20 (seq = 20, lseq = 20) | |
done |
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 <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#define THREADCOUNT 20 | |
/* compile with "gcc -o whyc why.c -lpthread" */ | |
void *maybeunique(void* arg) { | |
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; | |
static time_t last_start_time = 0; | |
static int seq = 0; | |
int lseq; | |
time_t now = time(NULL); | |
struct tm *info; | |
char dt[50]; | |
time_t rawtime; | |
pthread_mutex_lock(&mutex); | |
seq++; | |
if (seq > 59) { | |
seq = 0; | |
while (now == last_start_time) { | |
usleep(500000); | |
now = time(NULL); | |
} | |
} | |
last_start_time = now; | |
lseq = seq; //assign static value to thread-local variable | |
pthread_mutex_unlock(&mutex); | |
time(&rawtime); | |
info = localtime(&rawtime); | |
strftime(dt, sizeof(dt), "%Y-%m-%d_%H.%M.%S", info); | |
fprintf(stdout, "jobname.%s_%02d (seq = %02d, lseq = %02d)\n", dt, lseq, seq, lseq); | |
fflush(stdout); | |
return (void*) NULL; | |
} | |
int main(int argc, char** argv) { | |
int r; | |
pthread_t threads[THREADCOUNT]; | |
fprintf(stdout, "starting...\n"); | |
fflush(stdout); | |
for (int i = 0; i < THREADCOUNT; i++) { | |
r = pthread_create(&threads[i], NULL, &maybeunique, NULL); | |
if (r) { | |
fprintf(stderr, "failed to create thread %d, errno is %d\n", i, r); | |
} | |
} | |
fprintf(stdout, "threads dispatched. waiting...\n"); | |
fflush(stdout); | |
for (int i = 0; i < 5; i++) { | |
r = pthread_join(threads[i], NULL); | |
if (r) { | |
fprintf(stderr, "failed to wait on thread %d, errno is %d\n", i, r); | |
} | |
} | |
fprintf(stdout, "done\n"); | |
fflush(stdout); | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment