的文件中沒有說,中斷被重新觸發時線程B重新啓用中斷。我試過一個簡單的測試程序,可以確認你描述的行爲。
重新啓用中斷後,您可以檢查this_thread::interruption_requested()
以查看是否有中斷請求被禁用而中斷被禁用。如果確實要求中斷,你可以自己拋出一個thread_interrupted
異常。
以下是一個演示這一個工作程序:
#include <boost/thread.hpp>
#include <iostream>
using namespace std;
using namespace boost;
void threadB()
{
int ticks = 0;
this_thread::disable_interruption* disabler = 0;
try
{
while (ticks < 20)
{
if (ticks == 5)
{
cout << "Disabling interruptions\n";
disabler = new this_thread::disable_interruption;
}
if (ticks == 15)
{
cout << "Re-enabling interruptions\n";
delete disabler;
if (this_thread::interruption_requested())
{
cout << "Interrupt requested while disabled\n";
throw thread_interrupted();
}
}
cout << "Tick " << ticks << "\n";
thread::sleep(get_system_time() + posix_time::milliseconds(100));
++ticks;
}
}
catch (thread_interrupted)
{
cout << "Interrupted\n";
}
}
int main()
{
thread b(&threadB);
thread::sleep(get_system_time() + posix_time::milliseconds(1000));
b.interrupt();
cout << "main -> Interrupt!\n";
b.join();
}
希望這有助於。
我必須說boost :: thread變得很漂亮!之前,我不得不通過一個互斥保護的布爾變量手動實現自己的中斷機制。 – 2011-03-02 19:45:12