Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6116903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:15:35+00:00 2026-05-23T15:15:35+00:00

Are destructors for automatic objects guaranteed to execute if a thread is cancelled asynchronously?

  • 0

Are destructors for automatic objects guaranteed to execute if a thread is cancelled asynchronously?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:15:35+00:00Added an answer on May 23, 2026 at 3:15 pm

    In theory, it SHOULD work fine, but it’s worth testing with your platform.

    Cancelling a thread eventually ends up invoking pthread_exit(), which as far as I can tell from googling will invoke destructors. It does this by throwing some kind of ‘guaranteed uncaught’ exception all the way out to the thread wrapper, so all your stack-based objects get destructed in the correct order.

    See this page, for example. And this blog post:

    When calling pthread_exit() in C++, it
    has to destruct all objects that has
    been created on stack. This process
    called stack unwinding and this is
    exactly what happens when you throw an
    exception. pthread_exit() utilizes
    this feature of C++ to cleanup before
    shutting down the thread for good.

    To do that pthread_exit() throws some
    obscure exception and catches it right
    before ditching the thread. This way
    it cleans up all objects nicely. On
    the other hand, catching … becomes
    impossible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How does C++ ensure that destructors are called for stack assigned objects? What happens
The C++ standard 3.6.3 states Destructors for initialized objects of static duration are called
How does stl call the destructors of objects, as in std::vector::erase or std::vector::pop_back?
In C++ is a return value guaranteed to be created before automatic variables in
I ran a sample program and indeed destructors for stack-allocated objects are called, but
C++ automagically calls destructors of all local variables in the block in reverse order
I know it is a good practice to declare virtual destructors for base classes
Is there any good way to unit test destructors? Like say I have a
Does anyone know why the STL containers don't have virtual destructors? As far as
1) Refcounted pointers need stack variables to have constructors / destructors called at predictable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.