在用户级线程库中实现 join 函数
我正在尝试实现用户级线程库作为项目的一部分。我的重点是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下是取自 GnuPth(
pth_lib.c
) 的示例用户级线程库,分别显示了exit
和join
函数的实现。我简化了代码以突出显示返回值处理。以及相应的
pth_join
:Here is an example taken from the GnuPth(
pth_lib.c
) user-level threading library, which shows the implementation of theexit
andjoin
functions, respectively. I simplified the code to highlight the return value handling.and the corresponding
pth_join
:您可以在应用程序或进程上下文中为返回值创建存储,并在线程库初始化时进行初始化。您的 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.
我对实现中单个线程的元信息很感兴趣。返回值可以是线程中存储的项目之一,除了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.