Pass a global variable to a method as an argument

51 Views Asked by At

Global Variable:

int REGISTRATION_SIZE = 10;

I want to pass a global variable(REGISTRATION_SIZE) to wait_for_avaliable method as method argument like wait_for_avaliable(int size). How can I achieve that?

void wait_for_available_resource(struct Patient *p, pthread_mutex_t *registration_mutex, pthread_cond_t *register_cond){
    int rnd_wait_time;
    pthread_mutex_lock(registration_mutex);
    while(REGISTRATION_SIZE == 0){
        rnd_wait_time = generate_rnd_numb(WAIT_TIME) + 1;
        printf("Patient[%d] is waiting for an availabe desk.\n",p->thread_num);
        sleepForMs(rnd_wait_time);
        increase_meters(p);
        checkForRestOrCafe(p);
        pthread_cond_wait(register_cond,registration_mutex);
    }
    REGISTRATION_SIZE--;
    printf("Registration size:%d\n",REGISTRATION_SIZE);
    pthread_mutex_unlock(registration_mutex);
    
}

I tried with that way but it does not work:

void wait_for_available_resource(struct Patient *p, pthread_mutex_t *registration_mutex, pthread_cond_t *register_cond, int *size){
    int rnd_wait_time;
    pthread_mutex_lock(registration_mutex);
    while(size == 0){
        rnd_wait_time = generate_rnd_numb(WAIT_TIME) + 1;
        printf("Patient[%d] is waiting for an availabe desk.\n",p->thread_num);
        sleepForMs(rnd_wait_time);
        increase_meters(p);
        checkForRestOrCafe(p);
        pthread_cond_wait(register_cond,registration_mutex);
    }
    size--;
    printf("Registration size:%d\n",size);
    pthread_mutex_unlock(registration_mutex);
    
}
0

There are 0 best solutions below