函数可以在主应用程序中独立运行吗? [开放MP]
假设我们有一个不干扰其他数据并且独立运行的函数。但是,我听说一种方法是将整个应用程序放入一个部分,然后将其单独放入一个部分。是否可以通过主应用程序将其生成到线程中并确保主应用程序不会等待它结束来完成?
例如伪代码:
int main (void) {
<do stuff on thread 0>
<do stuff on thread 0>
<spawn independent function on thread 1 with no waiting>
<do stuff on thread 0>
<do stuff on thread 0>
}
编辑:它可以完全用另一种哲学来完成吗? (无 OpenMP)
Say we have a function that does not interfere with other data and runs independently. However, I heard a method to do it is to make the whole app in to a section and that alone a section. Can it be done instead with the main app spawning it into a thread and ensuring that the main app will not wait for it to end?
e.g. pseudo code:
int main (void) {
<do stuff on thread 0>
<do stuff on thread 0>
<spawn independent function on thread 1 with no waiting>
<do stuff on thread 0>
<do stuff on thread 0>
}
EDIT: Can it be done with another philosophy completely? (no OpenMP)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是 OpenMP 的主要目标:它是为并行处理而不是并发编程而构建的。检查本地线程库并查找所谓的“守护线程”、“分离线程”或类似的东西。
This isn't OpenMP's prime goal: it was built for parallel processing, not concurrent programming. Check your local thread library and look for something called "daemon threads", "detached threads" or similar.
如果您指的是分离线程,那么答案是肯定的。线程运行,然后退出,无需等待。您将无法从线程获取返回状态。
Larsmans 的帖子指出了为什么您可能不想使用线程
If you are referring to detached threads, then the answer is yes. The thread runs, then exits without having to be waited for. You lose the ability to get a return status from the thread.
Larsmans post points out why you probably do not want to use threading