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?
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.
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.
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.
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)).
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.).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With