0

全局变量:

int REGISTRATION_SIZE = 10;

我想将全局变量(REGISTRATION_SIZE)作为方法参数传递给 wait_for_avaliable 方法,例如 wait_for_avaliable(int size)。我怎样才能做到这一点?

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);
    
}

我尝试过这种方式,但它不起作用:

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);
    
}
4

0 回答 0