zk9099
zk9099

Reputation: 183

Detaching thread in C++11

I want to start a thread from a process and detach it and terminate the process. But the thread will be running continuously in the background. Can I achieve this with c++11 ?

I have detached my thread like this std::thread(&thread_func, param1, param2).detach();

But it gets terminated once the process is terminated.

Upvotes: 0

Views: 40

Answers (1)

freakish
freakish

Reputation: 56477

Detaching is not the same as running in the background. If you detach a thread then you simply tell the OS "I don't want to join the thread manually after it exits, please take care of that for me". But the OS will usually kill all child threads/processes when the main process exits.

So what you want is to run a deamon. However turning a process into a deamon (note that you can't daemonize a thread) is OS dependent. On linux you would call daemon function:

http://man7.org/linux/man-pages/man3/daemon.3.html

I don't know how to do that on Windows or other OS. Also you may want to read this:

Creating a daemon in Linux

Upvotes: 2

Related Questions