Can a function run independently inside a main app

2019-09-16 20:12发布

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)

2条回答
做自己的国王
2楼-- · 2019-09-16 20:35

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.

查看更多
老娘就宠你
3楼-- · 2019-09-16 20:37

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

查看更多
登录 后发表回答