我有两个测试用于中断boost :: thread。一个有效,另一个没有。谁能告诉我为什么?
工作:
#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";
}
像这样编译:{{1}}
输出是:
g++ test1.cpp -lboost_thread -lboost_system; ./a.out
断裂:
Run()
Thread: 0
Thread: 1
Main() sleeping
Thread: 2
Thread: 3
Main() interrupting the thread
INTERRUPTED!
Thread returning.
Main() bye!!
相同的编译,破坏的输出是:
#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";
}
因此,在我的破案中似乎没有中断。
答案 0 :(得分:3)
因为ThreadHandle
对象不是您为该线程启动的对象。
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.
}