试图将函数指针传递给pthread

时间:2015-04-20 22:50:24

标签: c pointers

我正在尝试使用函数指针的参数创建一个pthread,这里首先是将在pthread创建时调用的函数。

void *passenger(void *arguements){
        struct arg_struct *args = arguements;
        int passenger = args->p;
        int from_floor = args->f;
        int to_floor = args->t;
        void (*enter)(int,int) = args->en;
        void (*exit)(int,int) = args->ex;
        // wait for the elevator to arrive at our origin floor, then get in
        int waiting = 1;
        while(waiting){
                if(current_floor == from_floor && state == ELEVATOR_OPEN && occupancy==0) {
                        pthread_mutex_lock(&lock);
                        enter(passenger, 0);
                        occupancy++;
                        waiting=0;
                        pthread_mutex_unlock(&lock);
                }
        }

        // wait for the elevator at our destination floor, then get out
        int riding=1;
        while(riding) {
                if(current_floor == to_floor && state == ELEVATOR_OPEN){
                        pthread_mutex_lock(&lock);
                        exit(passenger, 0);
                        occupancy--;
                        riding=0;
                        pthread_barrier_wait(&barr);
                        pthread_mutex_unlock(&lock);
                }
        }
}

这是调用函数

void passenger_request(int passenger, int from_floor, int to_floor,void (*enter)(int,int), void(*exit)(int,int))
{
        pthread_mutex_lock(&passlock);
        struct arg_struct args;
        args.p = passenger;
        args.f = from_floor;
        args.t = to_floor;
        args.en = *enter;
        args.ex = *exit;
        pthread_create(&thread, NULL, &passenger, &args);
        //pthread_join(thread, NULL);
        pthread_mutex_unlock(&passlock);
        // wait for the elevator to arrive at our origin floor, then get in
}

当程序在启动时创建多个乘客时,该程序是分段错误,如果我注释掉pthread_create行没有发生崩溃。我假设这是我传递函数指针的参数的一个问题,但我很朦胧到底是怎么回事,因为所有这些指针都开始让我困惑。任何帮助都将非常感激

也是结构声明..

struct arg_struct{
        int p;
        int f;
        int t;
        void *(*ex)(int,int);
        void *(*en)(int,int);
};

2 个答案:

答案 0 :(得分:3)

args.en = *enter;
args.ex = *exit;

enterexit是函数指针。不要取消引用它们,而是直接通过args传递它们。也就是说,你需要:

args.en = enter;
args.ex = exit;

(假设你有正确定义的struct arg_struct未显示。

答案 1 :(得分:2)

您正在向新线程传递指向args的指针,该指针在passenger_request()函数的堆栈上定义。一旦passenger_request()返回,该内存可以被重用,覆盖或其他任何内容。它不再保证包含您放入的内容。然而你的线程仍然有一个指向它的指针,并可能继续尝试使用它。这可能会导致崩溃,尽管可能是间歇性的。

尝试使用args做一些不同的事情。如果你只需要它一次,你就可以把它变成全局的。如果您需要多个不同的,请使用malloc:

在堆上分配它
void passenger_request(int passenger, int from_floor, int to_floor,void (*enter)(int,int), void(*exit)(int,int))
{
        pthread_mutex_lock(&passlock);
        struct arg_struct *args = malloc(sizeof(struct arg_struct));
        args->p = passenger;
        args->f = from_floor;
        args->t = to_floor;
        args->en = enter;
        args->ex = exit;
        pthread_create(&thread, NULL, &passenger, args);
        //pthread_join(thread, NULL);
        pthread_mutex_unlock(&passlock);
        // wait for the elevator to arrive at our origin floor, then get in
}

然后在乘客()中,一旦你做得很好并真正完成了它,那就免费(args)。