I developed a multi threaded caching proxy for OpenWrt (Linux for routers, so there is few RAM available). I used Boost and Curl libraries, it works fine but its RAM consumption doesn't stop increasing (Memory leak).
I free all variables allocated dynamically
I close all the opened files
Clean up the initialized curl handles
Global variable are constants
Here is a part of my main code:
while (true) {
ip::tcp::socket* socket = new ip::tcp::socket(io_service);
acceptor.accept(*socket);
pthread_t thread;
pthread_create(&thread, NULL, handleRequest_thread, (void*) socket);
pthread_detach(thread);
}
It's sure that the memory leak occurs "handleRequest_thread", so my question is:
Is there a way to free all the resources (memory, opened files ...) after the thread is finished ?
You cannot free all resources automatically ( only by exiting your process, but that's not very good way ). So you have to control your recourses, use smart pointers where you can, automatically close files ( using fstream for example ) etc.
As for thread: maybe you should use boost.thread instead, it will make sure you don't forget to clean thread objects itself ( what you can easily forget using plain pthreads ).
Do you destroy the ip::tcp::socket* in the detached thread when it finishes? Memory for threads is freed on joining, but this is not neccessary for detached threads. The resources for detached thread are given back to the system when they finish. You could check the exit status of pthread_detach if you are able to detach the thread.
from the man pages man pthread_create:
A thread may either be joinable or detached. If a thread is joinable,
then another thread can call pthread_join(3) to wait for the thread to
terminate and fetch its exit status. Only when a terminated joinable
thread has been joined are the last of its resources released back to
the system. When a detached thread terminates, its resources are auto‐
matically released back to the system: it is not possible to join with
the thread in order to obtain its exit status.
Perhaps you create the threads faster then you can finish them, and hence your memory usage keeps increasing.
Related
Is this the equivalent of a leak? To use pointers and dynamically allocated memory as an analogy, if I lose a pointer to a resource that I've allocated memory for I can't delete or free that memory anymore. Likewise if I have a worker thread that's running an infinite loop and I detach the thread then is that a leak? There's no way to stop the thread executing if it's been detached?
What happens is the thread keeps running, potentially forever. If the thread decides on its own to exit, it will clean up its own internal data structures so there won't be any memory leak (at least, not from the thread-class itself; detaching the thread object tells it not to expect any parent thread to do cleanup via join(), so the exiting thread will do that cleanup itself). If it keeps running, then whatever resources it has allocated will remain allocated, although technically it's not a memory leak because code in the thread itself still (presumably) has the ability to free those resources, if it wanted to.
There is still a way to stop the thread from executing; you simply have to ask it nicely to exit. How you ask it is up to you -- you could do it by setting a std::atomic<bool> pleaseQuit; variable to true, and relying on the thread to periodically check the variable's value and exit if it's true, or by sending a byte to the thread on a pipe or a socket, or by some other means. This does require the co-operation of the code running inside the thread, of course; there's no good/safe way to unilaterally kill an uncooperative thread.
Once you've asked the thread to stop running, you usually want to then block until you know that it the thread is guaranteed dead, e.g. so that you can safely deallocate data structures that the thread might try to access while it still exists. To do that, you'll need to join() the thread, which means you shouldn't detach() it since detached threads can't be joined. Detached threads are only useful in situations where you don't care about cleaning up (e.g. because you're not expecting the thread to ever stop running anyway)
So I am using a variable
std::vector<pthread_t> preallocatedThreadsPool;
to hold all the pthreads,
then I use a for loop
preallocatedThreadsPool.resize(preallocatThreadsNumber); // create a threadpoOl
for(pthread_t i : preallocatedThreadsPool) {
pthread_create(&i, NULL, threadFunctionUsedByThreadsPool, NULL);
}
to create the threads pool,
the question is how do I really destory it, for example, when i send signal to the program then i need to manual handle the program to stop all the preallocated pthreads?
I have tried to use another for loop and inside the for loop to call pthread_exit(i),
but the IDE, tell me the for loop will only execute once, which obviously not working
I have tried to use preallocatedThreadsPool.clear() , to clean the vector, however when i use
gdb tool to debug it, inside the info threads , the threads are still there?
is there a good way to destory all the preallocated pthreads in my case?
Threads have to exit themselves. You can't exit another thread.
You can use pthread_join to wait for a thread to exit, or pthread_detach to say that you're never going to call pthread_join. You have to call one of these, or it leaks the thread. pthread_join destroys the thread; pthread_detach doesn't destroy the thread (obviously) but it allows the thread to destroy itself when it exits.
Since this is a thread pool, you must have a queue of things you want the threads in the pool to do. You can add special "please exit" items to the end of the queue (or the beginning), and then wait for the threads to exit. Make it so the threads exit when they see a "please exit" item in the queue.
It's all about thread synchronization. The proper way is that you have to have a global flag (a condition variable or a Win32 Event for example) that threads must periodically check and if set, terminate. When a thread is exiting, you must also wait for it to do so, so each thread should signal another event when "I'm done".
After that, any "handle" allocated to pthread or to std::thread or to CreateThread can be destroyed. In std::thread, you can detach and forget about the handle.
Even if you can kill the thread immediately by a function such as TerminateThread (there should be something similar in pthreads), this is very bad, for you will have leaked memory, possibly.
In my programme, I handle new threads with
pthread_t thread;
pthread_create(&thread, NULL,
c->someFunction, (void *) fd); //where fd is ID of the thread
The question is quite simple - if I just let the someFunction to finish, is it needed then in C++ to call something e.g. join or anything else, to prevenet memory leaks or is the memory freed automatically??
From the opengroup page for pthread_join,
The pthread_join() function provides a simple mechanism allowing an
application to wait for a thread to terminate. After the thread
terminates, the application may then choose to clean up resources that
were used by the thread. For instance, after pthread_join() returns,
any application-provided stack storage could be reclaimed.
The pthread_join() or pthread_detach() function should eventually be
called for every thread that is created with the detachstate attribute
set to PTHREAD_CREATE_JOINABLE so that storage associated with the
thread may be reclaimed.
and from the man page of pthread_join
Failure to join with a thread that is joinable (i.e., one that is not
detached), pro‐
duces a "zombie thread". Avoid doing this, since each zombie thread consumes some
system resources, and when enough zombie threads have accumulated, it will no longer
be possible to create new threads (or processes).
There is no pthreads analog of waitpid(-1, &status, 0), that is, "join with any ter‐
minated thread".
If you believe you need this functionality, you probably need to
rethink your application design.
If you do pthread_detach,
The pthread_detach() function shall indicate to the implementation
that storage for the thread thread can be reclaimed when that thread
terminates
If you don't detach or join a joinable thread, it can cause waste of resources
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?
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.).
I have a TCP Server application that serves each client in a new thread using POSIX Threads and C++.
The server calls "listen" on its socket and when a client connects, it makes a new object of class Client. The new object runs in its own thread and processes the client's requests.
When a client disconnects, i want some way to tell my main() thread that this thread is done, and main() can delete this object and log something like "Client disconnected".
My question is, how do i tell to the main thread, that a thread is done ?
The most straightforward way that I can see, is to join the threads. See here. The idea is that on a join call, a command thread will then wait until worker threads exit, and then resume.
Alternatively, you could roll something up with some shared variables and mutexes.
If the child thread is really exiting when it is done (rather than waiting for more work), the parent thread can call pthread_join on it which will block until the child thread exits.
Obviously, if the parent thread is doing other things, it can't constantly be blocking on pthread_join, so you need a way to send a message to the main thread to tell it to call pthread_join. There are a number of IPC mechanisms that you could use for this, but in your particular case (a TCP server), I suspect the main thread is probably a select loop, right? If that's the case, I would recommend using pipe to create a logical pipe, and have the read descriptor for the pipe be one of the descriptors that the main thread selects from.
When a child thread is done, it would then write some sort of message to the pipe saying "I'm Done!" and then the server would know to call pthread_join on that thread and then do whatever else it needs to do when a connection finishes.
Note that you don't have to call pthread_join on a finished child thread, unless you need its return value. However, it is generally a good idea to do so if the child thread has any access to shared resources, since when pthread_join returns without error, it assures you that the child thread is really gone and not in some intermediate state between having sent the "I'm Done!" message and actually having exited.
pthreads return 0 if everything went okay or they return errno if something didn't work.
int ret, joined;
ret = pthread_create(&thread, NULL, connect, (void*) args);
joined = pthread_join(&thread, NULL);
If joined is zero, the thread is done. Clean up that thread's object.
While it is possible to implement IPC mechanisms to notify a main thread when other threads are about to terminate, if you want to do something when a thread terminates you should try to let the terminating thread do it itself.
You might look into using pthread_cleanup_push() to establish a routine to be called when the thread is cancelled or exits. Another option might be to use pthread_key_create() to create a thread-specific data key and associated destructor function.
If you don't want to call pthread_join() from the main thread due to blocking, you should detach the client threads by either setting it as option when creating the thread or calling pthread_detach().
You could use a queue of "thread objects to be deleted", protect access to the queue with a mutex, and then signal a pthread condition variable to indicate that something was available on the queue.
But do you really want to do that? A better model is for each thread to just clean up after itself, and not worry about synchronizing with the main thread in the first place.
Calling pthread_join will block execution of the main thread. Given the description of the problem I don't think it will provide the desired solution.
My preferred solution, in most cases, would be to have the thread perform its own cleanup. If that isn't possible you'll either have to use some kind of polling scheme with shared variables (just remember to make them thread safe, hint:volatile), or perhaps some sort of OS dependant callback mechanism. Remember, you want to be blocked on the call to listen, so really consider having the thread clean itself up.
As others have mentioned, it's easy to handle termination of a given thread with pthread_join. But a weak spot of pthreads is funneling information from several sources into a synchronous stream. (Alternately, you could say its strong spot is performance.)
By far the easiest solution for you would be to handle cleanup in the worker thread. Log the disconnection (add a mutex to the log), delete resources as appropriate, and exit the worker thread without signaling the parent.
Adding mutexes to allow manipulation of shared resources is a tough problem, so be flexible and creative. Always err on caution when synchronizing, and profile before optimizing.
I had exactly the same problem as you described. After ~300 opened client connections my Linux application was not able to create new thread because pthread_join was never called. For me, usage of pthread_tryjoin_np helped.
Briefly:
have a map that holds all opened thread descriptors
from the main thread before new client thread is opened I iterate through map and call pthread_tryjoin_np for each thread recorded in map. If thread is done the result of call is zero meaning that I can clean up resources from that thread. At the same time pthread_tryjoin_np takes care about releasing thread resources. If pthread_tryjoin_np call returns number different from 0 this means that thread is still running and I simply do nothing.
Potential problem with this is that I do not see pthread_tryjoin_np as part official POSIX standard so this solution might not be portable.