C 在用户级线程库中实现连接函数

C 在用户级线程库中实现连接函数,c,multithreading,join,C,Multithreading,Join,我正在尝试将用户级线程库作为项目的一部分来实现。我的重点是连接函数。假设Thread1在Thread2上调用join函数。我需要做的是从Thread2获取提供给pthread_exit()的返回值/参数,并将其存储在join函数参数中指定的内存位置 但是,如何获取另一个线程的返回值 任何帮助都将不胜感激。谢谢我对您实现中单个线程的元信息很感兴趣。除了ID、堆栈指针等之外,返回值还可以是存储在线程中的项目之一 调用pthread_exit()时,调度程序应将此类信息转储到一个或多个数据结构中,以便

我正在尝试将用户级线程库作为项目的一部分来实现。我的重点是连接函数。假设Thread1在Thread2上调用join函数。我需要做的是从Thread2获取提供给pthread_exit()的返回值/参数,并将其存储在join函数参数中指定的内存位置

但是,如何获取另一个线程的返回值


任何帮助都将不胜感激。谢谢

我对您实现中单个线程的元信息很感兴趣。除了ID、堆栈指针等之外,返回值还可以是存储在线程中的项目之一


调用pthread_exit()时,调度程序应将此类信息转储到一个或多个数据结构中,以便其他线程可以在需要时查阅这些信息。

我对实现中单个线程的元信息很感兴趣。除了ID、堆栈指针等之外,返回值还可以是存储在线程中的项目之一


调用pthread_exit()时,调度程序应将此类信息转储到一个或多个数据结构中,以便其他线程可以在需要时查阅这些信息。

您可以在应用程序或进程上下文中为返回值创建存储,并在线程库初始化时初始化。您的pthread_出口将填充该值,您的join将使用threadid来检索它-我认为这只适用于非动态分配的返回值。

您可以在应用程序或进程上下文中为返回值创建存储,在线程库初始化时初始化。您的pthread_出口将填充该值,您的join将使用threadid来检索它-我认为这只适用于非动态分配的返回值。

下面是一个取自(
pth_lib.c
)用户级线程库的示例,它分别显示了
exit
join
函数的实现。I简化突出显示返回值处理的代码

void pth_exit(void *value)
{
    pth_debug2("pth_exit: marking thread \"%s\" as dead", pth_current->name);

    /* the main thread is special, because its termination
       would terminate the whole process, so we have to delay 
       its termination until it is really the last thread */

    /* execute cleanups */

    /*
     * Now mark the current thread as dead, explicitly switch into the
     * scheduler and let it reap the current thread structure; we can't
     * free it here, or we'd be running on a stack which malloc() regards
     * as free memory, which would be a somewhat perilous situation.
     */
    pth_current->join_arg = value;
    pth_current->state = PTH_STATE_DEAD;

    pth_debug2("pth_exit: switching from thread \"%s\" to scheduler", pth_current->name);

    //return (for ever) to the scheduler
}
以及相应的
pth_join

/* waits for the termination of the specified thread */
int pth_join(pth_t tid, void **value)
{      
    //Validate thread situation.

    //wait until thread death

    //save returned value for the caller
    if (value != NULL)
        *value = tid->join_arg;

    //remove thread from the thread queue

    //free its memory space

    return TRUE;
}

下面是一个取自(
pth_lib.c
)用户级线程库的示例,它分别显示了
exit
join
函数的实现。I简化突出显示返回值处理的代码

void pth_exit(void *value)
{
    pth_debug2("pth_exit: marking thread \"%s\" as dead", pth_current->name);

    /* the main thread is special, because its termination
       would terminate the whole process, so we have to delay 
       its termination until it is really the last thread */

    /* execute cleanups */

    /*
     * Now mark the current thread as dead, explicitly switch into the
     * scheduler and let it reap the current thread structure; we can't
     * free it here, or we'd be running on a stack which malloc() regards
     * as free memory, which would be a somewhat perilous situation.
     */
    pth_current->join_arg = value;
    pth_current->state = PTH_STATE_DEAD;

    pth_debug2("pth_exit: switching from thread \"%s\" to scheduler", pth_current->name);

    //return (for ever) to the scheduler
}
以及相应的
pth_join

/* waits for the termination of the specified thread */
int pth_join(pth_t tid, void **value)
{      
    //Validate thread situation.

    //wait until thread death

    //save returned value for the caller
    if (value != NULL)
        *value = tid->join_arg;

    //remove thread from the thread queue

    //free its memory space

    return TRUE;
}

这完全取决于实现的工作方式。“用户级线程”没有通用/可移植的方法。是的,这是正确的。我只是想找个办法来做这件事。我想不出要做什么。这完全取决于你的实现是如何工作的。“用户级线程”没有通用/可移植的方法。是的,这是正确的。我只是想找个办法来做这件事。我想不出要做什么。我不能将它存储在线程的元信息中,对吗?因为,一旦线程退出,我就必须释放与线程相关的信息。不,对于可连接的线程,在线程加入之前,您不能释放该元信息。否则(1)没有地方存储返回值,(2)相同的线程id可能会被重新用于新线程。我不能将其存储在线程的元信息中,对吗?因为,一旦线程退出,我就必须释放与线程相关的信息。不,对于可连接的线程,在线程加入之前,您不能释放该元信息。否则(1)没有地方存储返回值,(2)相同的线程id最终可能会被重新用于新线程。感谢您的响应。据我所知,在exit函数中,u不会释放与线程关联的上下文。但是,你实际上什么时候做?这可以在join函数中完成,但如果该线程从未被任何其他线程连接,该怎么办。在这种情况下什么时候释放?是的,Gnupth(不是我!)在
join
函数中释放线程数据。据我所知(如果我错了,请纠正我),POSIX接口强制执行
join
操作,除非设置了特定的线程属性。因此,您可以在
exit
中添加一个钩子,以在这种特殊情况下释放内存。感谢您的响应。据我所知,在exit函数中,u不会释放与线程关联的上下文。但是,你实际上什么时候做?这可以在join函数中完成,但如果该线程从未被任何其他线程连接,该怎么办。在这种情况下什么时候释放?是的,Gnupth(不是我!)在
join
函数中释放线程数据。据我所知(如果我错了,请纠正我),POSIX接口强制执行
join
操作,除非设置了特定的线程属性。因此,您可以在
exit
中添加一个钩子,以在这种特定情况下释放内存。