0

I want to be able to catch the end of a main function and perform additional operations in another thread before process termination in C++.

I have been trying to get the handle to the main thread and then join with it but seems std::this_thread/boost::this_thread do not allow you to get access to the handle for the current thread.

What I would like to do is basically the following:

void thread_function(thread_handler) {
  thread_handler.join();
  < Perform extra operations before the program finishes its main function >
}

int main() {
  < thread_function thread started in LD_PRELOAD>

  ....  Program runs .....
  return 0;
}

In my scenario these two functions have no shared state as the thread_function thread is invoked in a shared library using LD_PRELOAD but I can pass to it anything from the thread that later invokes main(). It may well be that it is not possible to catch the end of the main function but figured I'd ask in case anyone knows about this.

  • Why not just have a global object with a destructor? – Kerrek SB May 26 '14 at 22:33
  • 1
    Have you considered `::atexit` or `std::at_quick_exit` ? – Chnossos May 26 '14 at 22:38
  • @KerrekSB that's not the same. Many libraries which, themselves, use global statics, cannot be used outside main (the static initialization fiasco). So a global isn't the same as main (it looks like it should be enough for the OP's scenario though, indeed) – sehe May 26 '14 at 22:48
  • @Chnossos thanks a bunch! That's exactly what I needed and it seems like it works even when set prior to the main function being invoked. – libkafka May 26 '14 at 22:51
  • 2
    Hmm, it isn't actually different from your main() function taking care of ensuring that the threads exited, just before it quits. Just a way to obfuscate the problem, you just hope that the ::atexit() functions run in the right order. Order is *very* important. – Hans Passant May 26 '14 at 23:15

1 Answers1

-1

Sounds like you need a semaphore, after all, you need to prevent the program closing until all your threads have finished whatever they need to do at program exit.

So you need the main thread to signal that it's stopping, then wait for the other threads (if there are more than one) to finish and report back that they are done. Easiest way is to set an event to notify the other threads, then wait until each thread has signaled a semaphore set to the the of threads you're going to wait on.

Kennel objects will do fit this, they work cross process so they'll work across shared libraries.

gbjbaanb
  • 51,617
  • 12
  • 104
  • 148
  • A condition variable + `std::thread::join()` would be far more appropriate, since they're actually part of the language. – ildjarn May 27 '14 at 01:51
  • How do you figure? "*I can pass to it anything from the thread that later invokes `main()`.*" He doesn't need the handle of the main thread to pass a reference to a condition variable to the secondary thread from the thread that invokes `main`... – ildjarn May 27 '14 at 07:36
  • so what you're trying to say is to pass a reference to the thread started in main as a reference to the thread itself to join on? Why don't you post an answer explaining what you mean instead of a cryptic comment to mine. – gbjbaanb May 27 '14 at 07:43
  • No, I'm saying pass a reference to a condition variable, if passing a reference to one is needed at all – how did you expect a semaphore to be used? This isn't cryptic in the least. And the reason I posted a comment on your answer is because I was responding to the suggestion to use a semaphore when a condition variable is in the language and a semaphore is not. – ildjarn May 27 '14 at 07:45
  • still post an answer explaining it. I'd not heard of a condition variable in C++ before (new to C++11 apparently). No doubt the OP doesn't either. – gbjbaanb May 27 '14 at 07:50
  • A new answer isn't warranted; the only thing wrong with your answer IMO is the suggestion to use a semaphore. http://en.cppreference.com/w/cpp/thread/condition_variable – ildjarn May 27 '14 at 07:52