有没有办法“加入”? (阻塞)在 POSIX 线程中,而不退出加入者?

发布于 2024-08-31 04:26:05 字数 362 浏览 4 评论 0原文

我埋头于多线程/并行性文档中,试图弄清楚如何用我一直在设计的编程语言来实现线程实现。

我正在尝试将心理模型映射到 pthreads.h 库,但我遇到了一件事:我需要我的解释器实例在完成对 的解释后继续存在>routine(语言的闭包/函数数据类型),因为我想稍后将其他例程分配给它们进行解释,从而节省了线程和解释器设置/拆卸时间。

这很好,只是 pthread_join(3) 要求我调用 pthread_exit(3) 来“解锁”原始线程。如何阻塞原始线程(当它需要执行例程的结果时),然后在子例程的解释完成时取消阻塞?

I’m buried in multithreading / parallelism documents, trying to figure out how to implement a threading implementation in a programming language I’ve been designing.

I’m trying to map a mental model to the pthreads.h library, but I’m having trouble with one thing: I need my interpreter instances to continue to exist after they complete interpretation of a routine (the language’s closure/function data type), because I want to later assign other routines to them for interpretation, thus saving me the thread and interpreter setup/teardown time.

This would be fine, except that pthread_join(3) requires that I call pthread_exit(3) to ‘unblock’ the original thread. How can I block the original thread (when it needs the result of executing the routine), and then unblock it when interpretation of the child routine is complete?

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

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

发布评论

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

评论(2

梦途 2024-09-07 04:26:05

使用pthread_cond_t;在一个线程上等待它,并在另一个线程中发出信号或广播它。

Use a pthread_cond_t; wait on it on one thread and signal or broadcast it in the other.

变身佩奇 2024-09-07 04:26:05

听起来您实际上想要线程池模式的实现。它形成了一个相当简单的概念模型,无需重复的线程创建和线程创建。降低成本。一些操作系统直接支持它,而在其他操作系统上,使用队列和信号量来实现应该相当简单。

Sounds like you actually want an implementation of the Thread Pool Pattern. It makes for a fairly simple conceptual model, without repeated thread creation & tear down costs. Some OS's directly support it, on others it should be reasonably simple to implement using a queue and a semaphore.

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