Search code examples
c++boostboost-thread

Why can't I interrupt this particular boost::thread?


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.


Solution

  • 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.
    }