-
-
Save schn27/23b47563b429aaaad5ac315d05a43a11 to your computer and use it in GitHub Desktop.
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 <semaphore.h> | |
#include <stddef.h> | |
#include <stdio.h> | |
#include <time.h> | |
void *thread(void *arg) { | |
sem_t *sem = (sem_t *)arg; | |
for (size_t i = 0;; ++i) { | |
pthread_mutex_t mutexes[10]; | |
for (size_t j = 0; j < sizeof(mutexes) / sizeof(mutexes[0]); ++j) { | |
pthread_mutex_init(&mutexes[j], NULL); | |
} | |
for (size_t j = 0; j < sizeof(mutexes) / sizeof(mutexes[0]); ++j) { | |
pthread_mutex_destroy(&mutexes[j]); | |
} | |
if ((i % 100) == 0) { | |
sem_post(sem); | |
} | |
} | |
return NULL; | |
} | |
#define THREADS 4 | |
int main() { | |
pthread_t threads[THREADS]; | |
sem_t sems[THREADS]; | |
for (size_t i = 0; i < THREADS; ++i) { | |
sem_init(&sems[i], 0, 0); | |
pthread_create(&threads[i], NULL, thread, (void *)&sems[i]); | |
} | |
for (;;) { | |
for (size_t i = 0; i < THREADS; ++i) { | |
struct timespec ts; | |
clock_gettime(CLOCK_REALTIME, &ts); | |
ts.tv_sec += 1; | |
if (sem_timedwait(&sems[i], &ts) != 0) { | |
printf("\nthread #%d timeout\n", i); | |
return -1; | |
} | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment