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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:57:47+00:00 2026-06-08T16:57:47+00:00

I have two tests for interrupting a boost::thread. One works, the other doesn’t. Can

  • 0

I have two tests for interrupting a boost::thread. One works, the other doesn’t. Can anyone tell me why?

Working:

#include <iostream>
#include <string>
#include <boost/thread.hpp>
#include <boost/chrono.hpp>
#include <unistd.h>

using namespace std;

void Run(void)
{
    try
    {
        cout << "Run()\n";
        for ( int i = 0; i < 1000; i++ )
        {
            cout << "Thread: " << i << endl;
            boost::this_thread::sleep(boost::posix_time::milliseconds(500));
        }
    } catch (...)
    {
        cout << "INTERRUPTED!\n";
    }
    cout << "Thread returning.\n";
};

int main()
{
    boost::thread my_thread(Run);
    sleep(1);
    cout << "Main() sleeping\n";
    sleep(1);
    cout << "Main() interrupting the thread\n";
    my_thread.interrupt();
    sleep(1);
    cout << "Main() bye!!\n";
}

Compile like so: g++ test1.cpp -lboost_thread -lboost_system; ./a.out

Output is:

Run()
Thread: 0
Thread: 1
Main() sleeping
Thread: 2
Thread: 3
Main() interrupting the thread
INTERRUPTED!
Thread returning.
Main() bye!!

Broken:

#include <iostream>
#include <string>
#include <boost/thread.hpp>
#include <boost/chrono.hpp>
#include <unistd.h>

using namespace std;

class CThread
{
public:
    void Interrupt() { cout << "calling interrupt\n"; ThreadHandle.interrupt(); }

protected:
    static unsigned int Init(void * process);
    virtual int Run(void) =0;
    void StartThread(void);
    boost::thread ThreadHandle;
};

unsigned int CThread::Init(void * process)
{
    cout << "Init()\n";
    return ((CThread *)process)->Run();
}

void CThread::StartThread(void)
{
    boost::thread ThreadHandle(CThread::Init, this);
}

class my_thread_class : public CThread
{
    public:
    my_thread_class();

    int Run(void)
    {
       cout << "Run(), thread running\n";
        for ( int i = 0; i < 1000; i++ )
        {
            cout << "Thread: " << i << endl;
            boost::this_thread::sleep(boost::posix_time::milliseconds(200));
        }
        cout << "Thread returning.\n";
        return 0;
    };
};

my_thread_class::my_thread_class()
{
    StartThread();
}

int main()
{
    my_thread_class my_thread;
    sleep(1);
    cout << "Main() sleeping\n";
    sleep(2);
    cout << "Main() interrupting the thread\n";
    my_thread.Interrupt();
    sleep(5);
    cout << "Main() bye!!\n";
}

Same compilation, and the broken output is:

Init()
Run(), thread running
Thread: 0
Thread: 1
Main() sleeping
Thread: 2
Thread: 3
Main() interrupting the thread
calling interrupt
Thread: 4
Thread: 5
Main() bye!!

So it doesn’t appear to interrupt in my broken case.

  • 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-08T16:57:48+00:00Added an answer on June 8, 2026 at 4:57 pm

    Because the ThreadHandle object isn’t the one you started for the thread.

    class CThread
    {
    public:
        void Interrupt() { cout << "calling interrupt\n"; ThreadHandle.interrupt(); }
    
    protected:
        static unsigned int Init(void * process);
        virtual int Run(void) =0;
        void StartThread(void);
    
        // This is a member object
        boost::thread ThreadHandle;
    };
    
    void CThread::StartThread(void)
    {
        // This is _not_ the member object, you have just hidden
        // the member object with an automatic object of the same
        // name.  This works, because boost::thread doesn't stop
        // the thread when it goes out of scope, it just disconnects
        // boost::thread ThreadHandle(CThread::Init, this);
    
        // renamed to avoid hiding the member variable.
        boost::thread started_thread(CThread::Init, this);
    
        // Since you want your member object to actually represent the 
        // thread you started, you should be able to do this:
        ThreadHandle.swap(started_thread);
    
        // Now your member ThreadHandle, should be associated with the 
        // thread as you expected.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two Controller unit tests and each one sets an HttpClient metaclass execute
I have two set of integration tests in one maven project - automatic and
I'm running load tests for my application. I have two servers: one with my
I wrote some tests that use XML files. I have two project one with
In one of my integration tests, I have two threads that uninstall then install
I have two tests to check the expected exception throw. I am using Junit
Currently I have two tests that each individually test for a button_click within a
I have two unit tests that should share a lot of common tests with
I have written two short tests and compiled both with g++ -S (gcc version
I have two projects: a profiler and a basic application (with JUnit tests) The

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.