아무것도 스레드 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'가 꽤 매끈 해지고 있다고 말해야합니다! 이전에는 mutex가 보호하는 부울 변수를 사용하여 수동으로 자체 인터럽트 메커니즘을 구현해야했습니다. –