简体   繁体   English

试图将函数指针传递给pthread

[英]trying to pass function pointer to pthread

I'm trying to create a pthread with arguments for a function pointer, here first is the function that will be called on pthread creation.. 我正在尝试为函数指针的参数创建一个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);
                }
        }
}

and here is the calling function 这是调用函数

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
}

The program is seg faulting when it creates multiple passengers on initilization, if I comment out the pthread_create line no crashing occurs. 该程序在初始化时创建多个乘客时出现段错误,如果我注释掉pthread_create行,则不会发生崩溃。 I'm assuming it's an issue with my passing of arguments for the function pointers, but I'm hazy as to what exactly is going on as all these pointers are starting to confuse me. 我以为函数指针的参数传递是个问题,但是我不清楚到底发生了什么,因为所有这些指针都开始使我感到困惑。 Any help whatsoever would be much appreciated 任何帮助将不胜感激

also the struct declaration.. 也是struct声明

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

enter and exit are function pointers. enterexit是函数指针。 Don't dereference them but rather pass them straight through via args . 不要取消引用它们,而是通过args直接传递它们。 That is, you need: 也就是说,您需要:

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

(Assuming you have correct defined struct arg_struct which is not shown. (假设您已正确定义了未显示的struct arg_struct

You are passing your new thread a pointer to args, which is defined on the stack of your passenger_request() function. 您正在向新线程传递指向args的指针,该指针在您的passenger_request()函数的堆栈中定义。 As soon as passenger_request() returns, this memory could be reused, overwritten, or whatever. 一旦passenger_request()返回,此内存就可以重用,覆盖或进行其他操作。 It is no longer guaranteed to contain what you put in it. 它不再保证包含您放入的内容。 Yet your thread still has a pointer to it and may continue to try to use it. 但是您的线程仍然具有指向它的指针,并且可能会继续尝试使用它。 This is likely to cause a crash, although it may be intermittent. 尽管可能是间歇性的,但这很可能导致崩溃。

Try doing something different with args. 尝试对args做一些不同的事情。 If you only need it once, you could make it global. 如果只需要一次,则可以将其设置为全局。 If you need multiple different ones, then allocate it on the heap with malloc: 如果需要多个不同的对象,请使用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
}

Then in passenger() once you're well and truly done with it, free(args). 然后,一旦您完全正确地使用了passenger(),就可以释放(args)。

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

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