Changes between Version 2 and Version 3 of Ticket #7541
- Timestamp:
- Oct 23, 2012, 8:06:28 PM (10 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #7541 – Description
v2 v3 1 Based on the scoped_thread class defined in C++ Concurrency in Action define a thread wrapper class that instead of calling terminate if the thread is joinable on destruction, interrupts andjoin it if joinable.1 Based on the scoped_thread class defined in C++ Concurrency in Action define a thread wrapper class that instead of calling terminate if the thread is joinable on destruction, join it if joinable. 2 2 3 3 While the scoped_thread class defined in C++ Concurrency in Action is a strict scoped class that doesn't allows any change in the wrapped thread, I think that making the interface thread compatible is also a good option. This doesn't means that a strict scoped thread has no use. … … 6 6 namespace boost 7 7 { 8 class scoped_thread 8 class scoped_thread : public thread 9 9 { 10 t hread t;10 typedef thread base_type; 11 11 public: 12 BOOST_THREAD_NO_COPYABLE( scoped_thread) 12 BOOST_THREAD_MOVABLE_ONLY( scoped_thread ) 13 13 14 explicit scoped_thread(BOOST_THREAD_RV_REF(thread) t_) : 14 t(move(t_)) 15 base_type(boost::forward<base_type>(t_)), 16 m_(*static_cast<base_type*>(this)) 15 17 { 16 18 } 17 inline thread::id get_id() const BOOST_NOEXCEPT 19 20 scoped_thread(BOOST_RV_REF(scoped_thread) x) : 21 base_type(boost::move(static_cast<base_type&>(x))) 22 { } 23 24 scoped_thread& operator=(BOOST_RV_REF(scoped_thread) x) 18 25 { 19 return t.get_id(); 20 } 21 void swap(scoped_thread& x)BOOST_NOEXCEPT 22 { 23 t.swap(x.t); 24 } 25 void detach() 26 { 27 t.detach(); 28 } 29 void join() 30 { 31 t.join(); 32 } 33 template <class Rep, class Period> 34 bool try_join_for(const chrono::duration<Rep, Period>& rel_time) 35 { 36 return t.try_join_for(rel_time); 37 } 38 template <class Clock, class Duration> 39 bool try_join_until(const chrono::time_point<Clock, Duration>& abs_time) 40 { 41 return t.try_join_until(abs_time); 42 } 43 thread::native_handle_type native_handle() BOOST_NOEXCEPT { 44 return t.native_handle(); 45 } 46 void interrupt() 47 { 48 t.interrupt(); 26 base_type::operator=(boost::move(static_cast<base_type&>(x))); 27 return *this; 49 28 } 50 29 51 bool interruption_requested() const BOOST_NOEXCEPT52 {53 return t.interruption_requested();54 }55 30 ~scoped_thread() 56 31 { 57 t.interrupt(); 58 if (t.joinable()) 32 if (joinable()) 59 33 { 60 t.join();34 join(); 61 35 } 62 36 }