【Boost】boost库中thread多线程详解5——谈谈线程中断
生活随笔
收集整理的這篇文章主要介紹了
【Boost】boost库中thread多线程详解5——谈谈线程中断
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
線程不是在任意時刻都可以被中斷的。如果將線程中函數(shù)中的sleep()睡眠等待去掉,那么即使在主線程中調(diào)用interrupt()線程也不會被中斷。thread庫預定義了若干個線程的中斷點,只有當線程執(zhí)行到中斷點的時候才能被中斷,一個線程可以擁有任意多個中斷點。
2. thread::timed_join();
3. condition_variable::wait();
4. condition_variable::timed_wait();
5. condition_variable_any::wait();
6. condition_variable_any::timed_wait();
7. thread::sleep();
8. this_thread::sleep();
9. this_thread::interruption_point()
這些中斷點中的前8個都是某種形式的等待函數(shù),表明線程在阻塞等待的時候可以被中斷。 而最后一個位于子名字空間this_thread的interruption_point()則是一個特殊的中斷點函數(shù),它并不等待,只是起到一個標簽的作用,表示線程執(zhí)行到這個函數(shù)所在的語句就可以被中斷。看看下面的例子: namespace?? {?? ????boost::mutex?io_mu;?? ?? ????void?to_interrupt(const?std::string&?str)?? ????{?? ????????//?如果在線程外部調(diào)用了this_thread->interrupt()?? ????????//?線程內(nèi)部的以下這些檢查點可以拋出boost::thread_interrupted異常?? ????????try?? ????????{?? ????????????boost::this_thread::disable_interruption();?? ????????????for?(int?i?=?0;?i?<?5;?++i)?? ????????????{?? ????????????????boost::mutex::scoped_lock?lock(io_mu);?? ????????????????PRINT_DEBUG(i);?? ????????????????PRINT_DEBUG(std::boolalpha?<<?boost::this_thread::interruption_enabled());?? ????????????????//?PRINT_DEBUG(std::boolalpha?<<?boost::this_thread::interruption_requested());?? ????????????????if?(i?==?2)?? ????????????????{?? ????????????????????PRINT_DEBUG(std::boolalpha?<<?boost::this_thread::interruption_enabled());?? ????????????????????boost::this_thread::interruption_point();?? ????????????????????PRINT_DEBUG(std::boolalpha?<<?boost::this_thread::interruption_enabled());?? ????????????????}?? ????????????}?? ????????}?? ????????catch?(boost::thread_interrupted?&)?? ????????{?? ????????}?? ????}?????? }?? ?? void?test_thread_interrupt()?? {?? ????boost::thread?t(to_interrupt,?"hello");?? ????//?中斷函數(shù)?? ????t.interrupt();?? ????t.join();?? }??
運行結果:
[cpp]?view plaincopy print? 2013-01-02?11:00:44?263?[8272]?DEBUG?-?0?? 2013-01-02?11:00:44?266?[8272]?DEBUG?-?1?? 2013-01-02?11:00:44?269?[8272]?DEBUG?-?2??
如果注釋boost::this_thread::interrupt_point了,則結果如下:
[cpp]?view plaincopy print? 2013-01-02?11:02:06?555?[5168]?DEBUG?-?0?? 2013-01-02?11:02:06?559?[5168]?DEBUG?-?1?? 2013-01-02?11:02:06?561?[5168]?DEBUG?-?2?? 2013-01-02?11:02:06?564?[5168]?DEBUG?-?3?? 2013-01-02?11:02:06?567?[5168]?DEBUG?-?4??
下面談談啟用/禁用線程中斷
缺省情況下錢程都是允許中斷的,但thread庫允許控制線程的中斷行為。
thread 庫在子名字空間this_thread提供了一組函數(shù)和類來共同完成線程的中斷啟用和禁用:
1. interruption_enabled(): 函數(shù)檢測當前線程是否允許中斷
2. interruption_requested(): 函數(shù)檢測當前線程是否被要求中斷
3. 類disable_interruption是一個RAII類型的對象,它在構造時關閉線程的中斷,析構時自動恢復線程的中斷狀態(tài)。在disable_interruption 的生命期內(nèi)線程始終是不可中斷的,除非使用了restore_interruption 對象。
4. restore_interruption只能在disable_interruption 的作用域內(nèi)使用,它在構造時臨時打開線程的中斷狀態(tài),在析構時又關閉中斷狀態(tài)。
這些中斷點中的前8個都是某種形式的等待函數(shù),表明線程在阻塞等待的時候可以被中斷。
[cpp]?view plaincopy print? namespace?? {?? ????boost::mutex?io_mu;?? ?? ????void?to_interrupt_disable(const?std::string&?str)?? ????{?? ????????//?默認可以中斷?? ????????assert(boost::this_thread::interruption_enabled());?? ?????????? ????????for?(int?i?=?0;?i?<?10;?i++)?? ????????{?? ????????????//?關閉中斷?? ????????????boost::this_thread::disable_interruption?di;???????????? ????????????//?此時中斷不可用?? ????????????PRINT_DEBUG(std::boolalpha?<<?"interruption_enabled?=?"?<<??boost::this_thread::interruption_enabled());?? ????????????//?是否有中斷請求?? ????????????PRINT_DEBUG(std::boolalpha?<<?"interruption_requested?=?"?<<??boost::this_thread::interruption_requested());?? ?? ????????????boost::mutex::scoped_lock?lock(io_mu);?? ????????????PRINT_DEBUG(i);?? ????????????//?使用中斷點函數(shù),因為關閉中斷,此時無效果。?中斷恢復后,它才生效。?? ????????????boost::this_thread::interruption_point();?? ?? ????????????if?(i?==?8)?? ????????????{?? ????????????????//?臨時恢復中斷?? ????????????????boost::this_thread::restore_interruption?ri(di);?? ????????????????PRINT_DEBUG(std::boolalpha?<<?"interruption_enabled?=?"?<<??boost::this_thread::interruption_enabled());?? ????????????????PRINT_DEBUG(std::boolalpha?<<?"interruption_enabled?after?restore?=?"?<<??boost::this_thread::interruption_enabled());?? ????????????????boost::this_thread::interruption_point();?? ????????????}?? ????????}?? ????}?? }?? ?? void?test_thread_interrupt_disable()?? {?? ????boost::thread?t(to_interrupt_disable,?"hello");?? ????t.interrupt();?? ????t.join();?? }??
結果:
[cpp]?view plaincopy print? 2013-01-02?14:09:35?538?[7628]?DEBUG?-?interruption_enabled?=?false?? 2013-01-02?14:09:35?544?[7628]?DEBUG?-?interruption_requested?=?true?? 2013-01-02?14:09:35?551?[7628]?DEBUG?-?0?? 2013-01-02?14:09:35?555?[7628]?DEBUG?-?interruption_enabled?=?false?? 2013-01-02?14:09:35?563?[7628]?DEBUG?-?interruption_requested?=?true?? 2013-01-02?14:09:35?570?[7628]?DEBUG?-?1?? 2013-01-02?14:09:35?574?[7628]?DEBUG?-?interruption_enabled?=?false?? 2013-01-02?14:09:35?581?[7628]?DEBUG?-?interruption_requested?=?true?? 2013-01-02?14:09:35?586?[7628]?DEBUG?-?2?? 2013-01-02?14:09:35?589?[7628]?DEBUG?-?interruption_enabled?=?false?? 2013-01-02?14:09:35?601?[7628]?DEBUG?-?interruption_requested?=?true?? 2013-01-02?14:09:35?608?[7628]?DEBUG?-?3?? 2013-01-02?14:09:35?614?[7628]?DEBUG?-?interruption_enabled?=?false?? 2013-01-02?14:09:35?621?[7628]?DEBUG?-?interruption_requested?=?true?? 2013-01-02?14:09:35?627?[7628]?DEBUG?-?4?? 2013-01-02?14:09:35?630?[7628]?DEBUG?-?interruption_enabled?=?false?? 2013-01-02?14:09:35?637?[7628]?DEBUG?-?interruption_requested?=?true?? 2013-01-02?14:09:35?643?[7628]?DEBUG?-?5?? 2013-01-02?14:09:35?646?[7628]?DEBUG?-?interruption_enabled?=?false?? 2013-01-02?14:09:35?650?[7628]?DEBUG?-?interruption_requested?=?true?? 2013-01-02?14:09:35?655?[7628]?DEBUG?-?6?? 2013-01-02?14:09:35?659?[7628]?DEBUG?-?interruption_enabled?=?false?? 2013-01-02?14:09:35?663?[7628]?DEBUG?-?interruption_requested?=?true?? 2013-01-02?14:09:35?667?[7628]?DEBUG?-?7?? 2013-01-02?14:09:35?670?[7628]?DEBUG?-?interruption_enabled?=?false?? 2013-01-02?14:09:35?679?[7628]?DEBUG?-?interruption_requested?=?true?? 2013-01-02?14:09:35?685?[7628]?DEBUG?-?8?? 2013-01-02?14:09:35?689?[7628]?DEBUG?-?interruption_enabled?=?true?? 2013-01-02?14:09:35?695?[7628]?DEBUG?-?interruption_enabled?after?restore?=?true??
Interruption機制:
可以通過thread對象的interrupt函數(shù),通知線程,需要interrupt。線程運行到interruption point就可以退出。
Interruption機制舉例:
#include "stdafx.h" #include <iostream> #include <boost/thread.hpp> using namespace std;void f() {for(int i=1;i<0x0fffffff;i++){if(i%0xffffff==0){cout<<"i="<<((i&0x0f000000)>>24)<<endl;cout<<"boost::this_thread::interruption_requested()="<<boost::this_thread::interruption_requested()<<endl;if(((i&0x0f000000)>>24)==5){boost::this_thread::interruption_point();}}} }int _tmain(int argc, _TCHAR* argv[]) {boost::thread t(f);t.interrupt();t.join(); //等待線程結束return 0; }t.interrupt();告訴t線程,現(xiàn)在需要interrupt。boost::this_thread::interruption_requested()可以得到當前線程是否有一個interrupt請求。若有interrupt請求,線程在運行至interruption點時會結束。boost::this_thread::interruption_point();就是一個interruption point。Interruption point有多種形式,較常用的有boost::this_thread::sleep(boost::posix_time::seconds(5));當沒有interrupt請求時,這條語句會讓當前線程sleep五秒,若有interrupt requirement線程結束。
如何使線程在運行到interruption point的時候,不會結束,可以參考下面的例子:
#include "stdafx.h" #include <iostream> #include <boost/thread.hpp> using namespace std;void f() {for(int i=1;i<0x0fffffff;i++){if(i%0xffffff==0){cout<<"i="<<((i&0x0f000000)>>24)<<endl;cout<<"boost::this_thread::interruption_requested()"<<boost::this_thread::interruption_requested()<<endl;if(((i&0x0f000000)>>24)==5){boost::this_thread::disable_interruption di;{boost::this_thread::interruption_point();}}}} }int _tmain(int argc, _TCHAR* argv[]) {boost::thread t(f);t.interrupt();t.join(); //等待線程結束return 0; }注意boost::this_thread::disable_interruption這條語句的使用,它可以使大括號內(nèi)的interruption point不會中斷當前線程。
thread庫預定義了共9個中斷點,它們都是函數(shù),如下:
1. thread::join();2. thread::timed_join();
3. condition_variable::wait();
4. condition_variable::timed_wait();
5. condition_variable_any::wait();
6. condition_variable_any::timed_wait();
7. thread::sleep();
8. this_thread::sleep();
9. this_thread::interruption_point()
這些中斷點中的前8個都是某種形式的等待函數(shù),表明線程在阻塞等待的時候可以被中斷。 而最后一個位于子名字空間this_thread的interruption_point()則是一個特殊的中斷點函數(shù),它并不等待,只是起到一個標簽的作用,表示線程執(zhí)行到這個函數(shù)所在的語句就可以被中斷。看看下面的例子:
[cpp]?view plaincopy print?
[cpp]?view plaincopy print?
缺省情況下錢程都是允許中斷的,但thread庫允許控制線程的中斷行為。
thread 庫在子名字空間this_thread提供了一組函數(shù)和類來共同完成線程的中斷啟用和禁用:
1. interruption_enabled(): 函數(shù)檢測當前線程是否允許中斷
2. interruption_requested(): 函數(shù)檢測當前線程是否被要求中斷
3. 類disable_interruption是一個RAII類型的對象,它在構造時關閉線程的中斷,析構時自動恢復線程的中斷狀態(tài)。在disable_interruption 的生命期內(nèi)線程始終是不可中斷的,除非使用了restore_interruption 對象。
4. restore_interruption只能在disable_interruption 的作用域內(nèi)使用,它在構造時臨時打開線程的中斷狀態(tài),在析構時又關閉中斷狀態(tài)。
這些中斷點中的前8個都是某種形式的等待函數(shù),表明線程在阻塞等待的時候可以被中斷。
[cpp]?view plaincopy print?
[cpp]?view plaincopy print?
Interruption機制:
可以通過thread對象的interrupt函數(shù),通知線程,需要interrupt。線程運行到interruption point就可以退出。
Interruption機制舉例:
#include "stdafx.h" #include <iostream> #include <boost/thread.hpp> using namespace std;void f() {for(int i=1;i<0x0fffffff;i++){if(i%0xffffff==0){cout<<"i="<<((i&0x0f000000)>>24)<<endl;cout<<"boost::this_thread::interruption_requested()="<<boost::this_thread::interruption_requested()<<endl;if(((i&0x0f000000)>>24)==5){boost::this_thread::interruption_point();}}} }int _tmain(int argc, _TCHAR* argv[]) {boost::thread t(f);t.interrupt();t.join(); //等待線程結束return 0; }t.interrupt();告訴t線程,現(xiàn)在需要interrupt。boost::this_thread::interruption_requested()可以得到當前線程是否有一個interrupt請求。若有interrupt請求,線程在運行至interruption點時會結束。boost::this_thread::interruption_point();就是一個interruption point。Interruption point有多種形式,較常用的有boost::this_thread::sleep(boost::posix_time::seconds(5));當沒有interrupt請求時,這條語句會讓當前線程sleep五秒,若有interrupt requirement線程結束。
如何使線程在運行到interruption point的時候,不會結束,可以參考下面的例子:
#include "stdafx.h" #include <iostream> #include <boost/thread.hpp> using namespace std;void f() {for(int i=1;i<0x0fffffff;i++){if(i%0xffffff==0){cout<<"i="<<((i&0x0f000000)>>24)<<endl;cout<<"boost::this_thread::interruption_requested()"<<boost::this_thread::interruption_requested()<<endl;if(((i&0x0f000000)>>24)==5){boost::this_thread::disable_interruption di;{boost::this_thread::interruption_point();}}}} }int _tmain(int argc, _TCHAR* argv[]) {boost::thread t(f);t.interrupt();t.join(); //等待線程結束return 0; }注意boost::this_thread::disable_interruption這條語句的使用,它可以使大括號內(nèi)的interruption point不會中斷當前線程。
總結
以上是生活随笔為你收集整理的【Boost】boost库中thread多线程详解5——谈谈线程中断的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Boost】boost库中thread
- 下一篇: 【Boost】boost库中thread