函数可以在主应用程序中独立运行吗? [开放MP]

发布于 2024-10-04 12:32:19 字数 408 浏览 4 评论 0原文

假设我们有一个不干扰其他数据并且独立运行的函数。但是,我听说一种方法是将整个应用程序放入一个部分,然后将其单独放入一个部分。是否可以通过主应用程序将其生成到线程中并确保主应用程序不会等待它结束来完成?

例如伪代码:

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 技术交流群。

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

发布评论

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

评论(2

星星的轨迹 2024-10-11 12:32:19

这不是 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.

残龙傲雪 2024-10-11 12:32:19

如果您指的是分离线程,那么答案是肯定的。线程运行,然后退出,无需等待。您将无法从线程获取返回状态。

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

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