How pthread_join() determine that called/waiting thread is terminated - c

i am new in Multithreading. in pthread_join() function we put the main thread on wait till the called thread is not terminated. So my question is here how pthread_join() verify that called thread is terminated.

pthread_join is a portable interface around an operating system facility that instructs the OS scheduler to suspend the calling thread until the target thread has communicated that it has completed (either by returning from its entry point function or because pthread_exit has been called). The necessary wrapping logic and state set up by pthread_create. Essentially, you are waiting for a flag to change, but that's all handled for you by the pthreads library.

Related

Multithreading in C/C++ without waiting for the thread to finish

All the examples that I have seen about multithreading uses this method in the main method to wait until the thread is done:
pthread_join(thread_id, NULL);
But what if I don't want it to wait? I want my main function to continue as the thread is doing it's work, but at the same time, I don't want main to exit before the thread exists. Is this possible in C/C++?
If you want to avoid using pthread_join(), then pthread_detach() is an option.
From man-page:
int pthread_detach(pthread_t thread);
The pthread_detach() function marks the thread identified by thread
as detached. When a detached thread terminates, its resources are
automatically released back to the system without the need for
another thread to join with the terminated thread.
it does not prevent the thread from being
terminated if the process terminates using exit(3) (or equivalently,
if the main thread returns).

Do you have to close the thread once it has ended when using pthreads in Linux?

In WinAPI, if you create a thread and this thread is then ended, you still have to call CloseHandle() on the thread handle.
When using pthreads in Linux, do you also have to close the thread after it has ended?
The equivalent in pthreads is to call pthread_join() on the thread (this will block until the thread exits, if it hasn't already).
You also have the option to detach the thread with pthread_detach() (or create it detached using the attr argument to pthread_create() with pthread_attr_setdetachstate()), which means the thread no longer needs to be joined.

When thread release its resources

In man page of pthread_detach i read that when any thread is detached then at a time of thread termination it release its resources back to system.
What are thread resources?Is that it is a part of memory used by that thread, if it is so then that memory is a part of a process address space. I am trying to understand this but i dint got it.
And what about the joinable thread, when does thread release its resources? at a time of pthread_join or at a time of termination or process?
When resources are released in pthread_cancel command.
Every thread consumes some amount of bookkeeping resources in the operating system, as well as its own execution stack in userspace memory. Those resources are freed when the thread is destroyed, which can occur under several conditions, such as:
pthread_join returns when called on a joinable thread,
a detached thread's entry function returns,
main returns normally or exit is called,
the process is terminated due to receiving an unhandled signal,
exec is called successfully.
It is possible, however, to exit the thread that is running main and leave other, detached threads running. To do so, you have to call pthread_exit in the main thread.

Can I close/terminate an running thread from its thread function?

I have created a thread, with a custom thread function. I have a condition in the thread function that if it becomes true, I want to close the thread from inside the thread function.
Is it possible?
You can return from the thread and if you want to return some value, you can use pthread_join on that thread.
I assume you are using pthread for the thread functionlaity. You can call the pthread_detach() function in your custom thread function after creating the thread. In the created thread just returning from the thread function will be sufficient to close the thread and release all the resources associated with the thread.
For PThreads there are two ways to end a thread cleanly.
Detached the thread using pthread_detach(). To end it then call pthread_exit(). To find a thread's phtread-id from inside the thread itself use phtread_self().
Call pthread_exit() and have another thread call pthread_join() on the pthread-id received when creating the thread that ended.
If you miss to call pthread_join() on a thread not having been detached by calling pthread_detach(), the resources in use by the thread will not be released, even after the thread ended.
This could lead to a shortage on memory and/or other system resources. Take care this does not happen.
A third was to end a thread is to just cancel it using pthread_cancel(), which typically isn't initated by the thread itself (as I could just use one of the two ways described above to end itself), but from another thread in the situation where the thread to end is not aware of this and could not be notified to do so.
The need to cancel a thread should rarely arise, and if it does one might start to overthink the program's design.

POSIX threads and exiting from a thread

I have two threads, communicating with each other; each thread employs 'while(1) ..'. Now I need to let the threads exit upon a specific condition met, and therefore finish the application.
My question: is it safe to just 'return (NULL)' from the thread, or do I have to use 'pthread_exit' or 'pthread_join' functions as well?
It is safe to return null from the thread functions; the code that waits for them should be OK.
POSIX says of pthread_exit():
An implicit call to pthread_exit() is made when a thread other than the thread in which main() was first invoked returns from the start routine that was used to create it.
You do need something to wait for the thread with pthread_join() unless the thread was created with the detached attribute or detached later with pthread_detach().
Calling pthread_exit(NULL) and returning NULL at the end of the thread's initial function should be equivalent. However, doing either of these alone will lead to a resource leak. To avoid that, you must either call pthread_join on the thread from another thread, or put the thread in the detached state by calling pthread_detach on it or setting it to start in the detached state before creating it.

Resources