0

I am porting C++ library from Windows to Linux. This is multi-threaded library. In Windows version there are __try/__finally block used for freeing resources and other activity after thread end. GCC doesn't support __try/__finally and C++ standard doesn't support it, because C++ have RAII. But how can i apply it to thread function?(destructor for function??). I need to do some work then thread ended no matter which way (exception or normal return).

Is there any way to do this without writing catch(...) with "destructor" code and adding "destructor" code to all returns?

exbluesbreaker
  • 2,160
  • 3
  • 18
  • 30
  • I'm not sure I understand your problem. I always use join() method from boost::thread to wait for particular thread end. You can also think about at_thread_exit() free method, which is called for current thread. EDIT: uuuups, I supposed you use boost library. – Bogdan Feb 15 '14 at 08:44
  • A __finally block will run for *any* exception, not just C++ exceptions. There's no notion of this in Linux, it uses signals. So using try/catch instead is as close as you ought to get. – Hans Passant Feb 15 '14 at 10:02
  • 1
    Use [Boost.ScopeExit](http://www.boost.org/doc/libs/1_55_0/libs/scope_exit/doc/html/index.html) - it will make RAII for your. – Igor R. Feb 15 '14 at 16:23

0 Answers0