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

  • SEARCH
  • Home
  • 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 8726569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:16:25+00:00 2026-06-13T08:16:25+00:00

What happens when a deadline timer is destroyed during a context switch when one

  • 0

What happens when a deadline timer is destroyed during a context switch when one thread was already inside the timeout handler?

For example, if during execution of TimerCallback a context switch to another thread deleted ScheduledCommand?

ScheduledCommand::ScheduledCommand(
        const boost::shared_ptr<CommandInterface> command,
        const boost::posix_time::time_duration timeTillExecution):
    mTimer(TheCommandTimerThread::instance()->IoService(), timeTillExecution),
    mCommand(command)
{
    mTimer.async_wait(boost::bind(&ScheduledCommand::TimerCallback,
                                  this,
                                  boost::asio::placeholders::error));
}

ScheduledCommand::~ScheduledCommand()
{
    Cancel();
}

void ScheduledCommand::TimerCallback(const boost::system::error_code& error)
{
    if (!error)
    {
        assert(mCommand);
        mCommand->Execute();
    }
}

The above code had a segmentation fault at mCommand->Execute(). GDB analysis showed that mCommand was invalid. Probably deleted from another thread.
Thanks.

Edit:

Why doesn’t the following change fix this problem?

ScheduledCommand::Cancel()
{
    if (mTimer.cancel() == 0) 
    { 
        mTimer.wait() 
    } 
}
  • 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-06-13T08:16:27+00:00Added an answer on June 13, 2026 at 8:16 am

    If, in a multithreaded environment, you delete an object in one thread, while using it another, you can get a crash, exactly as you are seeing.

    When using boost::asio, if you cancel the timer, you still have to let it execute the handler (which will get an error that the operation was canceled) before you can safely destroy the object. Depending on the rest of your setup, there are a handful of approaches. Manual use of synchronization primatives, use of boost::asio::strand to limit some interactions to a single thread, or use of boost::shared_ptr to ensure that key objects remain viable until they are no longer referenced.

    Without more information, it’s difficult to tell you the best option.

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

Sidebar

Related Questions

What happens when a thread is put to sleep by other thread, possible by
What happens in the following example? struct B { }; struct D1 : B
What happens during a display mode change (resolution, depth) on an ordinary computer? (classical
Something happens in my system on a worker thread. Let's say that the state
What happens if we forcefully kill a running thread I have a thread namely
So here's the deal: I've got a JPanel and a JTextArea inside that one.
What happens if you try to add an item to a Collection that already
What happens if I submit an app and then (for example after 3 days)
What happens inside Java's ArrayList<T> (and probably many other classes) is that there is
What happens if I call [alloc] init] on an object which already was initialized

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.