int main(int argc, char * argv[]) { context_t * context; pthread_t * readers; pthread_t * writers; int check; int a; int arg; int NUM_READERS = 5; int NUM_WRITERS = 2; int timeout = -1; for(arg=1;arg<argc;arg++) { if (!strcmp(argv[arg], "--num-readers")) { arg++; if (arg == argc) { fprintf(stderr, "--num-readers accepts an argument!\n"); exit(-1); } NUM_READERS = atoi(argv[arg]); } else if (!strcmp(argv[arg], "--num-writers")) { arg++; if (arg == argc) { fprintf(stderr, "--num-writers accepts an argument!\n"); exit(-1); } NUM_WRITERS = atoi(argv[arg]); } else if (!strcmp(argv[arg], "--timeout")) { arg++; if (arg == argc) { fprintf(stderr, "--timeout accepts an argument!\n"); exit(-1); } timeout = atoi(argv[arg]); } else { fprintf(stderr, "Unknown option - \"%s\"!\n", argv[arg]); exit(-1); } } num_active_mutex = initial_mutex_constant; pthread_mutex_init(&num_active_mutex, NULL); readers = malloc(sizeof(readers[0])*NUM_READERS); writers = malloc(sizeof(writers[0])*NUM_WRITERS); mylock = pthread_rwlock_fcfs_alloc(); for(a=0;a<NUM_READERS;a++) { context = malloc(sizeof(*context)); context->index = a; check = pthread_create( &readers[a], NULL, reader_thread, context ); if (check != 0) { fprintf(stderr, "Could not create Reader #%i!\n", a); exit(-1); } } for(a=0;a<NUM_WRITERS;a++) { context = malloc(sizeof(*context)); context->index = a; check = pthread_create( &writers[a], NULL, writer_thread, context ); if (check != 0) { fprintf(stderr, "Could not create Writer #%i!\n", a); exit(-1); } } if (timeout < 0) { while(1) { sleep(1); } } else { int local_num_active = 1; sleep(timeout); stop = 1; while (local_num_active) { usleep(500); pthread_mutex_lock(&num_active_mutex); local_num_active = num_active_threads; pthread_mutex_unlock(&num_active_mutex); } } pthread_rwlock_fcfs_destroy(mylock); for(a=0;a<NUM_READERS;a++) { pthread_join(readers[a], NULL); } for(a=0;a<NUM_WRITERS;a++) { pthread_join(writers[a], NULL); } free(readers); free(writers); return 0; }
void delete_lock(void * lock) { pthread_rwlock_fcfs_t * lk; lk = (pthread_rwlock_fcfs_t *)lock; pthread_rwlock_fcfs_destroy(lk); }