微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Windows上使用WaitForSingleObject,但支持提升线程中断

提升线程具有“可中断”的便利特性。 该框架在睡觉时引入中断点等。但是,使用阻止Win32调用绕过此function。 例如, WaitForSingleObject会阻塞一个线程,但不会被boost线程的中断机制中断。@H_502_1@

有什么方法来包装WaitForSingleObject或告诉boost等待一个Win32事件句柄,以便我可以重新获得中断点?@H_502_1@

为什么我不能处理NMI?@H_502_1@

诠释13h在Windows保护模式?@H_502_1@

中断描述符表(IDT)修改@H_502_1@

Linux:信号处理器可以被抢占吗?@H_502_1@

Interrupt Handler使用哪个堆栈 – Linux@H_502_1@

detail::win32::interruptible_wait实现这一点。@H_502_1@

正如你所看到的,它等待3个句柄(除了由调用者指定的句柄之外还有2个句柄)来表示中断。@H_502_1@

具体见@H_502_1@

WaitForMultipleObjectsEx调用@H_502_1@

该块@H_502_1@

else if(notified_index==interruption_index) { detail::win32::ResetEvent(detail::get_current_thread_data()->interruption_handle); throw thread_interrupted(); }@H_502_1@

作为参考, 升压许可证 :@H_502_1@

bool interruptible_wait(detail::win32::handle handle_to_wait_for,detail::timeout target_time) { detail::win32::handle handles[3]={0}; unsigned handle_count=0; unsigned wait_handle_index=~0U; #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS unsigned interruption_index=~0U; #endif unsigned timeout_index=~0U; if(handle_to_wait_for!=detail::win32::invalid_handle_value) { wait_handle_index=handle_count; handles[handle_count++]=handle_to_wait_for; } #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS if(detail::get_current_thread_data() && detail::get_current_thread_data()->interruption_enabled) { interruption_index=handle_count; handles[handle_count++]=detail::get_current_thread_data()->interruption_handle; } #endif detail::win32::handle_manager timer_handle; #ifndef UNDER_CE #if !BOOST_PLAT_WINDOWS_RUNTIME unsigned const min_timer_wait_period=20; if(!target_time.is_sentinel()) { detail::timeout::remaining_time const time_left=target_time.remaining_milliseconds(); if(time_left.milliseconds > min_timer_wait_period) { // for a long-enough timeout,use a waitable timer (which tracks clock changes) timer_handle=CreateWaitableTimer(NULL,false,NULL); if(timer_handle!=0) { LARGE_INTEGER due_time=get_due_time(target_time); bool const set_time_succeeded=SetWaitableTimer(timer_handle,&due_time,false)!=0; if(set_time_succeeded) { timeout_index=handle_count; handles[handle_count++]=timer_handle; } } } else if(!target_time.relative) { // convert short absolute-time timeouts into relative ones,so we don't race against clock changes target_time=detail::timeout(time_left.milliseconds); } } #endif #endif bool const using_timer=timeout_index!=~0u; detail::timeout::remaining_time time_left(0); do { if(!using_timer) { time_left=target_time.remaining_milliseconds(); } if(handle_count) { unsigned long const notified_index=detail::win32::WaitForMultipleObjectsEx(handle_count,handles,using_timer?INFINITE:time_left.milliseconds,0); if(notified_index<handle_count) { if(notified_index==wait_handle_index) { return true; } #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS else if(notified_index==interruption_index) { detail::win32::ResetEvent(detail::get_current_thread_data()->interruption_handle); throw thread_interrupted(); } #endif else if(notified_index==timeout_index) { return false; } } } else { detail::win32::sleep(time_left.milliseconds); } if(target_time.relative) { target_time.milliseconds-=detail::timeout::max_non_infinite_wait; } } while(time_left.more); return false; }@H_502_1@

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐