#include #include #include #include typedef struct { int idx; int *potato; pthread_mutex_t *mtx; } data; void *f(void *arg) { data *d = (data*) arg; while(1) { pthread_mutex_lock(d->mtx); // *((*d).potato) if(*(d->potato) < 0) { pthread_mutex_unlock(d->mtx); break; } int sub = rand() % 91 + 10; printf("Thread with id %d has subtracted %d from %d\n", d->idx, sub, *(d->potato)); *(d->potato) -= sub; if(*(d->potato) < 0) { printf("Thread with id %d has exploded!\n", d->idx); pthread_mutex_unlock(d->mtx); break; } pthread_mutex_unlock(d->mtx); int sleep = rand() % 101 + 100; usleep(sleep * 1000); } return NULL; } int main(int argc, char *argv[]){ if(argc != 2) { perror("Please provide exactly one argument!\n"); exit(0); } int n = atoi(argv[1]); srand(getpid()); int potato = rand() % 9001 + 1000; printf("Main has generated %d\n", potato); pthread_t th[n]; data *args = malloc(sizeof(data) * n); pthread_mutex_t mtx; pthread_mutex_init(&mtx, NULL); for(int i = 0; i < n; i++) { args[i].idx = i + 1; args[i].potato = &potato; args[i].mtx = &mtx; pthread_create(&th[i], NULL, f, (void *) &args[i]); } for(int i = 0; i < n; i++) { pthread_join(th[i], NULL); } pthread_mutex_destroy(&mtx); free(args); return 0; }