在用户级线程库中实现 join 函数

发布于 2024-12-06 14:50:31 字数 187 浏览 0 评论 0原文

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

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

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

I am trying to implement a user level thread library as part of a project. My focus is on the join function. Lets say that Thread1 calls join function on Thread2. What i need to do is get the return value/argument supplied to pthread_exit() from Thread2 and store it in the memory location specified in join function argument.

But, how do i get the return value of another thread?

Any help will be appreciated. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

段念尘 2024-12-13 14:50:31

以下是取自 GnuPth(pth_lib.c) 的示例用户级线程库,分别显示了 exitjoin 函数的实现。我简化了代码以突出显示返回值处理。

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

Here is an example taken from the GnuPth(pth_lib.c) user-level threading library, which shows the implementation of the exit and join functions, respectively. I simplified the code to highlight the return value handling.

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
}

and the corresponding 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;
}
凡间太子 2024-12-13 14:50:31

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

You could create storage for the return value in the application or process context, initialized when your threads library initializes. Your pthread_exit would fill the value and your join would use the threadid to retrieve it - I think this will only work for non-dynamically allocated return values though.

自由范儿 2024-12-13 14:50:31

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

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

I'm interesting about the meta-info for individual thread in you implementation. Return value could be one of items stored in that of a thread, besides ID, stack pointer and so on.

When pthread_exit() is called, such information should be dumped to one or more data structures by scheduler, so that other thread can consult them when needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文