在我的應用程序中,我給了10秒的睡眠時間。我已經使用boost :: this_thread :: sleep函數進行睡眠。 是否有任何可能的方法來中斷boost :: this_thread :: sleep函數。如何在C++中中斷boost :: this_thread
回答
拋出:
boost::thread_interrupted
如果當前執行線程被中斷。注:
sleep()
是預定義interruption points之一。
因此,你需要在你的線程做的是把sleep()
中的呼叫try-catch
,並趕上boost::thread_interrupted
。然後在線程對象上調用interrupt()
來中斷睡眠。
順便說一句,也從sleep
reference:自從3.0.0
警告
已過時。而不是使用
sleep_for()
和sleep_until()
。
hey @Joachim Pileborg我使用來自「boost :: this_thread」的睡眠函數,它沒有中斷API,我的要求是使用「boost :: this_thread」,所以有什麼方法來中斷睡眠。 – Mukul
@ user3721279'boost :: this_thread :: sleep'是,正如我在我的答案中的鏈接引用的一個*中斷*點。請閱讀我答案中提供的鏈接。 –
您可以使用interrupt()
中斷sleep
功能。
你必須爲你的線程啓用中斷,當它休眠時,你可以使用中斷功能。
升壓竟對此一個非常好的簡短的教程在網上,看看這裏:http://www.boost.org/doc/libs/1_54_0/doc/html/thread/thread_management.html#thread.thread_management.tutorial.interruption
我已經使用boost :: this_thread :: sleep函數給出了睡眠。有沒有什麼可能的方法來中斷boost :: this_thread :: sleep函數。?
不這樣,但不難實現它,在boost::this_thread::sleep
術語:
void sleep_for(boost::posix_time::milliseconds interval, std::atomic<bool>& interrupted)
{
static const auto resolution = boost::posix_time::milliseconds(10));
ptime end_time(microsec_clock::local_time()) + interval;
while(!interrupted.load())
{
boost::this_thread::sleep(resolution);
if(microsec_clock::local_time() > end_time)
break;
}
}
客戶機代碼的線程1:
std::atomic<bool>& interrupt = false;
void f() {
sleep_for(boost::posix_time::milliseconds(200), interrupt);
}
客戶機代碼的線程2:
interrupt.store(true); /// will cause thread 1 to be interrupted, if called
/// during execution of sleep_for.
- 1. 如何將boost :: this_thread :: get_id()轉換爲int?
- 2. 未定義引用`boost :: this_thread:
- 3. 禁用中斷時中斷boost :: thread
- 4. Boost :: thread瞬時中斷
- 5. 未定義的引用`boost :: this_thread :: interruption_point()'
- 6. 的boost :: this_thread :: disable_interruption對象使用混亂
- 7. 在阻塞boost C++方法中,如何在Python中捕獲中斷信號?
- 8. Boost Thread - 如何確認中斷
- 9. 如何停止/中斷boost :: thread?
- 10. 可以使用boost :: threads中的std :: this_thread *函數嗎?
- 11. 如何使用boost :: asio從網絡中斷中恢復如何使用boost :: asio
- 12. 如何在C++中使用boost :: dynamic_bitset
- 13. 中斷分離的boost ::線程
- 14. Boost(v1.33.1)線程中斷
- 15. 如何模擬C/C++中的中斷?
- 16. 如何判斷boost :: split_iterator是否在字符串中間開始?
- 17. 在VS2013中std :: this_thread :: get_id()可靠嗎?
- 18. 如何睡覺一個C++ Boost線程
- 19. 如何中斷Visual C++中的主線程
- 20. 如何解析使用Boost ptree中C++
- 21. boost :: thread_group在C++ 11中?
- 22. 如何在c#中找到斷點?
- 23. 如何在Visual C++中截斷文件?
- 24. 如何在C#中截斷列表?
- 25. 爲什麼boost :: barrier等待不是中斷點?
- 26. 當跨越DLL邊界時,Boost線程中斷不起作用
- 27. Boost :: thread :: interrupt()對不同的中斷點有不同的表現。爲什麼?
- 28. 替代std :: this_thread :: sleep_for()
- 29. 在boost線程上調用抽象函數,在interrupt_point時不會中斷
- 30. 奇怪的boost :: this_thread ::睡眠行爲
@WouterHuysentruit,你應該回答好笑嗎? – Nidhoegger