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 6529371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:37:40+00:00 2026-05-25T09:37:40+00:00

I have blocking task which will be performed by find_the_question() function. However, I do

  • 0

I have blocking task which will be performed by find_the_question() function. However, I do not want thread executing this function take more than 10 seconds. So in case it takes more than 10 seconds, I want to close that thread with cleaning all the resources.

I tried to write a code for that, but somehow I am not able to get a interrupt in find_the_question() function if thread takes more than 10 seconds. Could you please tell me what am I doing wrong?

void find_the_question(std::string value)
{
    //allocate x resources
    try{
        //do some process on resources
            sleep(14);
        //clean resources
    }
    catch(boost::thread_interrupted const& )
    {
        //clean resources
        std::cout << "Worker thread interrupted" << std::endl;
    }
}

int main()
{
        boost::posix_time::time_duration timeout = boost::posix_time::milliseconds(10000);
        std::cout << "In main" << std::endl;
        boost::thread t1(find_the_question, "Can you block me");

        t1.interrupt();
        if (t1.timed_join(timeout))
        {
          //finished
          std::cout << "Worker thread finished" << std::endl;
        }
        else
        {
          //Not finished;
          std::cout << "Worker thread not finished" << std::endl;
        }

        std::cout << "In main end" << std::endl;
}

Output:
If t1 takes more than 10 seconds to complete, I am getting following console output.

std::cout << "In main" << std::endl;
std::cout << "Worker thread not finished" << std::endl;
std::cout << "In main end" << std::endl;

whereas, I am expecting following output

std::cout << "In main" << std::endl;
std::cout << "Worker thread interrupted" << std::endl;
std::cout << "Worker thread not finished" << std::endl;
std::cout << "In main end" << std::endl;

Could you please tell me what am I doing wrong.

Thanks in advance

  • 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-25T09:37:41+00:00Added an answer on May 25, 2026 at 9:37 am

    For using boost::thread::interrupt(), you have to use boost::thread::sleep() for it to work.

    A running thread can be interrupted by invoking the interrupt() member
    function of the corresponding boost::thread object. When the
    interrupted thread next executes one of the specified interruption
    points (or if it is currently blocked whilst executing one) with
    interruption enabled, then a boost::thread_interrupted exception will
    be thrown in the interrupted thread. If not caught, this will cause
    the execution of the interrupted thread to terminate. As with any
    other exception, the stack will be unwound, and destructors for
    objects of automatic storage duration will be executed

    Predefined interruption points:

    The following functions are interruption points, which will throw
    boost::thread_interrupted if interruption is enabled for the current
    thread, and interruption is requested for the current thread:

    boost::thread::join()
    boost::thread::timed_join()
    boost::condition_variable::wait()
    boost::condition_variable::timed_wait()
    boost::condition_variable_any::wait()
    boost::condition_variable_any::timed_wait()
    boost::thread::sleep()
    boost::this_thread::sleep()
    boost::this_thread::interruption_point()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application processing network communication with blocking calls. Each thread manages a
We have a (currently InnoDB) table which contains roughly 500,000 rows. This represents a
I'm trying to make a basic multiprocessing task and this is what I have.
I have mainly CPU intensive operation which is running on a thread pool. Operation
According to MSDN you have to create a non-blocking socket like this: unsigned nonblocking
I have a service that performs a slow task, when its finished I want
I'm implementing some rudimentary SQL Server monitoring to watch for excessive blocking. I have
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have you managed to get Aptana Studio debugging to work? I tried following this,
I have a TCP server that creates a (blocking) socket, waits until it is

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.