#include #include #include #include #include typedef struct { pthread_mutex_t m; } tick_arg; void *tick(void *arg) { pthread_mutex_t m = ((tick_arg *)arg)->m; pthread_mutex_lock(&m); printf("tick │ %04ld │\n", time(NULL)%1000); sleep(1); pthread_mutex_unlock(&m); return NULL; } typedef struct { pthread_mutex_t *m; } tock_arg; void *tock(void *arg) { pthread_mutex_t *m = ((tock_arg *)arg)->m; pthread_mutex_lock(m); printf("tock │ %04ld │\n", time(NULL)%1000); sleep(1); pthread_mutex_unlock(m); return NULL; } int main(int argc, const char *argv[]) { pthread_mutex_t tick_lock, tock_lock; pthread_mutex_init(&tick_lock, NULL); pthread_mutex_init(&tock_lock, NULL); int n = argc > 1 ? atoi(argv[1]) : 5; pthread_t *id = (pthread_t *)calloc(n, sizeof(pthread_t)); tick_arg *tick_args = (tick_arg *)calloc(n, sizeof(tick_arg)); tock_arg *tock_args = (tock_arg *)calloc(n, sizeof(tock_arg)); puts(" ┌──────┐"); for(size_t i=0; i