Kumar Sourav
Kumar Sourav

Reputation: 21

How to run a function in background?

I want to run a C function in background but I want it to keep it running when the main function exits.

I tried following:

  1. pthread_detach, a detached thread exits if the main function calls exit() instead of pthread_exit.
  2. deamon(): It runs the code in background but not in parallel.

So, what is the simplest way to run a C function in parallel/background even after my main function exits?

Upvotes: 2

Views: 2066

Answers (2)

Ivan  Ivanov
Ivan Ivanov

Reputation: 2106

Once main returns OS will delete all threads within process. To continue thread use fork to create child process.

Upvotes: 1

alk
alk

Reputation: 70971

So, what is the simplest way to run a C function in parallel/background even after my main function exits?

Exit main() by calling pthread_exit().

Upvotes: 1

Related Questions