Automatically releasing mutexes held when thread destructor runs - pthreads

Is there a foolproof way to automatically release mutexes held by a thread when that thread is exiting (in its destructor)?
The approach I have been taking is to create a structure for each mutex which hold the identity of the thread that holds it, and then in the destructor to scan through this list and if any mutexes match the thread being finished, to release it then. But I'm thinking that this actually has a race condition: what happens if after I lock the mutex but before I set the data structure the destructor is called?
I've also looked at pthread_mutexattr_setrobust_np, but my understanding is that np functions are non-portable, and I have had issues with that in the past.
For reference, each thread is associated with a TCP/IP connection, and locking/unlocking occurs in response to requests over this connection. If the connection abnormally closes I need to clean up i.e. release any locks held.

I found a solution which appears to work. First, I use an error checking mutex (PTHREAD_ERRORCHECK_MUTEX_INITIALIZER or PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP).
Next, in the destructor, I trying to unlock all mutexes, with the idea being any mutex not owned by the thread will be left alone, but any mutex owned by the thread will be released.
For some reason even mutexes owned by the thread return EPERM, but a subsequent attempt to re-lock the mutex from another thread succeeds whereas without trying to unlock another attempt will deadlock. Conversely, other mutexes not owned by the destructed thread are still found to be locked after the destructor runs.

Related

Synchronizing forked processes with pthread_mutex in C

Is it possible to use mutex from pthread.h to synchronize processes created with fork() from unistd.h? Afaik, both in the end are using system call clone().
I am asking it in the scope of shared memory segment (from ipc.h, shm.h) with critical data, which should be protected against concurrent writes from different processes. In that memory then semaphores can be defined and later used in different processes. Why couldn't mutexes be used instead of semaphores?
Why am I asking?
First of all I was told that it won't work, without receiving any explanation for that. On the Internet I was not able to find any answer so I decided to ask here.
Second, forked process is safer than thread created with pthread_create - if forked process crashes, the rest of the program continues to work and if thread crashes then whole program exits.
Third, mutexes seem to be more human-friendly than semaphores in managing.

Concerning PTHREAD_MUTEX_ROBUST

According to the official doc (emphasis mine):
PTHREAD_MUTEX_ROBUST
If the process containing the owning thread of
a robust mutex terminates while holding the mutex lock, the next
thread that acquires the mutex shall be notified about the termination
by the return value [EOWNERDEAD] from the locking function. If the
owning thread of a robust mutex terminates while holding the mutex
lock, the next thread that acquires the mutex may be notified about
the termination by the return value [EOWNERDEAD]...
The doc seems to be differentiating specifically the cases for out-of-process and in-process thread termination and have chosen the wording shall and may carefully. Does this imply that the robustness is mandatory in the out-of-process case, but is optional in the in-process case (which may result in a deadlock)?

Pthread library cleanup of detached threads

I'm trying to figure out how NPTL cleans up the resources (stack space, etc) of a
detached thread when it exits. Joinable threads are easy, there's a call to pthread_join
which waits for a specific thread to exit and then reclaims its resources. No problem, but
how does NPTL know that a detached thread has exited? Does anyone know or at least know who
would know?
thanks,
Rich
after the thread function returns, NPTL will check whether the thread is detached, if it is detached, it will free everything of the thread directly, if it is joined, it will leave thread control block(TCB) to pthread_join to free TCB.
for detail, http://raison.gegahost.net/?p=91 may helps.

Does pthread_exit kill a thread.. I mean free the stack allocated to it?

I want to create a lot of threads for a writing into a thread, and after writing I call exit... But, when I call exit do I free up the stack or do I still consume it??
In order to avoid resource leaks, you have to do one of these 2:
Make sure some other thread call pthread_join() on the thread
Create the thread as 'detached', which can either be done by setting the proper pthread attribute to pthread_create, or by calling the pthread_detach() function.
Failure to do so will often result in the entire stack "leaking" in many implementations.
The system allocates underlying storage for each thread, (thread ID, thread retval, stack), and this will remain in the process space (and not be recycled) until the thread has terminated and has been joined by other threads.
If you have a thread which you don't care how the thread terminates, and a detached thread is a good choice.
For detached threads, the system recycles its underlying resources automatically after the thread terminates.
source article: http://www.ibm.com/developerworks/library/l-memory-leaks/

Delphi - What happens with un-freed (but terminated) thread when application exits?

I have multithreaded application and I've got a little problem when application ends: I can correctly terminate the thread by calling TThread.Terminate method in Form1.OnDestroy event handler, but the termination does take some time and so I can't free the memory (by TThread.Free method).
Unfortunately for some other reason I must have TThread.FreeOnTerminate property set to false, so the thread object isn't destroyed automatically after thread termination.
My question is probably a little silly and I should have known it a long time ago, but is this ok and the thread will be destroyed automatically (since the application just ends), or is it a problem and the memory would be "lost"? Thanks a lot for explanation.
You should wait for the thread to terminate before you begin the process off shutting down the rest of your application, otherwise shared resources may be freed under the threads feet, possibly leading to a string of access violations. After you have waited for thread termination, then you can free it. In fact, that's what the TThread destructor does for you.
If there are no shared resources, then sure, let it die by itself. Even if the thread terminates after the main thread, all that is required is that all your threads exit for the program to terminate. Any memory associated with the thread's object will just get cleaned up and given back to the OS with everything else.
BUT, be careful! If your thread is taking a while to exit, it can lead to a zombie process sitting there churning away without a GUI. That is why it is very important to check the Terminated flag very often in the thread loop, and exit the thread.
N#
Your question is not silly or simple - read the MSDN article. All in all, if you want to be on the safe side you are better to wait a background thread to terminate before exiting an application.
The thread will eventually terminate and Windows will clean up any memory left over. However, you might as well just wait for the thread to terminate, because that is exactly what Windows will do anyway. Your application may appear to have shut down because all windows may have been closed/hidden, but the application process won't terminate until all threads have finished...
When a process terminates the OS will reclaim all allocated memory and will close all open handles. You don't need to worry about MEMORY*) that leaks in the very special event of shutting down the application. The OS will also close all your open handles**), at least theoretically. All those taken into account, it might be safe for you to simply terminate your thread (using TerminateThread(MyThread.Handle)) from your forms destructor, before killing other shared resources. Ask yourself those questions:
What's the thread doing? Is it safe to terminate it at any time? Example: If the thread is doing any writing to disk, it's unsafe to just kill it, because you might live files on disk in an inconsistant state.
Are you using any resources that aren't automatically freed by Windows? Can't think of an good example here...
If you're on the safe side with both, you can use TerminateThread and not wait for the thread to naturally terminate. A safer approach might a combined approach, maybe you should give the thread a chance to naturally terminate and, if it didn't terminate withing 5 seconds, force-terminate it.
*) I'm talking about memory you can prove only leaks on process termination, like threads you kill without giving them a chance to properly shut down, or global singleton classes you don't free. All other unaccounted memory needs to be tracked down and fixed, because it's an bug.
**) Unfortunately the Windows OS is not bug-free. Example: Anyone that worked with serial devices on the Windows platform knows how easy it is to get the serial-device in a "locked" state, requiring an restart to get it working again. Technically that's also an Handle, end-processing the application that locked it should unlock it.
why you dont increment a variable when creating the thread, and on the destroy event wait until thread finish, decrement the variable, and on applicationterminate just do Application.processmessages ?
why your thread isn't freeonterminate=true ? all shared resources can be handled into a critical section.
best regards,

Resources