繁体   English   中英

试图将函数指针传递给pthread

[英]trying to pass function pointer to pthread

我正在尝试为函数指针的参数创建一个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声明

struct arg_struct{
        int p;
        int f;
        int t;
        void *(*ex)(int,int);
        void *(*en)(int,int);
};
args.en = *enter;
args.ex = *exit;

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

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

(假设您已正确定义了未显示的struct arg_struct

您正在向新线程传递指向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
}

然后,一旦您完全正确地使用了passenger(),就可以释放(args)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM