text: Q:
  
  Is there any pthread function which calls something when the last thread terminates?
  
  Suppose I have created 10 pthreads using a for loop in a C++ application on Linux. All these 10 threads are calling the same thread function. I want the last exiting thread (between [1..10]) to call a function where I can clear some data. Is there any pthread function like that? (Not talking about thread specific data and pthread_once())
  Note: Requirement is not with std::thread here.
  
  A:
  
  I don't know about a specific function for that, but you can simply join all 10 threads in a loop. Once all threads have joined, you can execute the function.  You can do this in the thread that spawned the 10 threads or in a separate thread if you so prefer. 
  
  i had to detach all the threads
  
  That's not a very good choice if you want to wait for them to exit.
  But you can work around it by using a shared array of 10 condition variables. Each of the threads should set the variable corresponding to it and call pthread_cond_signal. The waiter should use pthread_cond_wait in a loop to wait for each of the condition variables to be set.
  
  In this case, main() calls func1(), func1() creates a pthread_t pointer which points to arrays of pthread_ts( pthread_t *tid = new pthread_t[10]). And creates 10 threads in for loop. After creation, control will be returned to main(), main() has other tasks to do. Now, the problem is how can i remove (tid) from heap memory? 
  
  You can simply do as I suggested. Don't detatch the threads, but wait for the threads to join in a loop. Once they're finished, you are free to destroy the thread objects. You can do this in a separate thread as I said. No need for the main thread to wait anything.
  
  