2012-07-27 55 views
3

我有两个中断boost :: thread的测试。一个工作,另一个不工作。谁能告诉我为什么?为什么我不能中断这个特定的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"; 
} 

编译像这样: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"; 
} 

同一编译和破碎输出为:

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!! 

所以它不会出现在我的破情况下中断。

回答

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. 
} 
+0

是啊......发布后它迫使我重新阅读我的代码。我只做了C++几天,所以对于“魔法”的工作原理我还是有点不稳定。我应该存储一个指针参考吗?我可以创建一个&引用它吗? – 2012-07-27 15:29:30

+0

我已经更新了我的答案。你可以存储一个指针(我建议'boost :: scoped_ptr'或'std :: unique_ptr'),或者使用上面描述的'swap'方法。 – Chad 2012-07-27 15:32:04

+0

交换方法有效。感谢您帮助我,非常感谢。在星期五完成工作并完成工作是件好事。 ;) – 2012-07-27 15:55:27

相关问题