Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting a daemon thread only after it returns

I'm working on a project in which I have a main thread and one daemon thread to perform file outputs. In my main thread I have a field pthread_t * _daemon that I would like to delete, but obviously only after _daemon returns NULL (I understand that using pthread_exit() cause memory leaks).

How can I do it without busy-wait? If I try to use a condition variable I have a problem - When I call signal() form _daemon to wake up my main thread, it deletes _daemon before it _daemon returns NULL.

What I did is to just use a mutex lock that is locked when the program is launched and unlocked before _daemon returns. Is there any better way that would not cause busy wait?

like image 864
yotamoo Avatar asked May 13 '12 09:05

yotamoo


People also ask

How do I terminate a daemon thread?

If you call System. gc() explicitly and the JVM option -XX:+DisableExplicitGC is not set, then the daemon thread will exit. This means Garbage Collector is responsible for shutting down the daemon threads after all user threads finish.

What is daemon thread in C++?

A detached thread runs in the background. The ownership and control are passed over to the C++ Runtime Library. The C++ Runtime Library ensures that the resources of the thread are correctly reclaimed when the thread exits. Detached threads are also called daemon threads.

What is daemon thread in Linux?

A “daemon” thread is one that is supposed to provide a general service in the background as long as the program is running, but is not part of the essence of the program. Thus, when all of the non-daemon threads complete, the program is terminated.


2 Answers

pthread_detach() does what you're looking for. It sounds like it will solve your problem (no leaking) with a lot less complexity!

So you can safely call pthread_detatch(_daemon) when you're done with it inside the other thread, without having to worry about if the thread itself is still running. It does not terminate the thread, instead it causes the thread to be cleaned up when it does terminate.

From the documentation:

The pthread_detach() function shall indicate to the implementation that storage for the thread thread can be reclaimed when that thread terminates. If thread has not terminated, pthread_detach() shall not cause it to terminate.

You can actually create a thread in the detached state to start with by setting attr of:

   int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                      void *(*start_routine) (void *), void *arg);

appropriately at creation time. The pthread_create() manpage says:

When a detached thread terminates, its resources are automatically released back to the system. [snip] Making a thread detached is useful for some types of daemon threads whose exit status the application does not need to care about. By default, a new thread is created in a joinable state, unless attr was set to create the thread in a detached state (using pthread_attr_setdetachstate(3)).

like image 142
Flexo Avatar answered Sep 30 '22 14:09

Flexo


pthread_t is an unsigned long int, so I don't really see a need to make a pointer of pthread_t (you can as well use it's address in pthread functions), but if you insist, then you can delete it after creating a pthread (but then you will have no way to communicate with it, because it's a number of created thread - used for joining, detaching functions etc.).

like image 32
zos Avatar answered Sep 30 '22 15:09

zos