Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r81289 - in trunk: boost/thread/detail libs/thread/doc libs/thread/example
From: vicente.botet_at_[hidden]
Date: 2012-11-10 19:57:08


Author: viboes
Date: 2012-11-10 19:57:07 EST (Sat, 10 Nov 2012)
New Revision: 81289
URL: http://svn.boost.org/trac/boost/changeset/81289

Log:
Thread: ref #7669
Text files modified:
   trunk/boost/thread/detail/thread_group.hpp | 42 +++++++++++++++++++++++++++++++++++
   trunk/libs/thread/doc/thread_ref.qbk | 44 +++++++++++++++++++++++++++++++-----
   trunk/libs/thread/example/thread_group.cpp | 47 +++++++++++++++++++++++++++++++++++++++
   3 files changed, 125 insertions(+), 8 deletions(-)

Modified: trunk/boost/thread/detail/thread_group.hpp
==============================================================================
--- trunk/boost/thread/detail/thread_group.hpp (original)
+++ trunk/boost/thread/detail/thread_group.hpp 2012-11-10 19:57:07 EST (Sat, 10 Nov 2012)
@@ -36,6 +36,41 @@
             }
         }
 
+ bool is_this_thread_in()
+ {
+ thread::id id = this_thread::get_id();
+ boost::shared_lock<shared_mutex> guard(m);
+ for(std::list<thread*>::iterator it=threads.begin(),end=threads.end();
+ it!=end;
+ ++it)
+ {
+ if ((*it)->get_id() == id)
+ return true;
+ }
+ return false;
+ }
+
+ bool is_thread_in(thread* thrd)
+ {
+ if(thrd)
+ {
+ thread::id id = thrd->get_id();
+ boost::shared_lock<shared_mutex> guard(m);
+ for(std::list<thread*>::iterator it=threads.begin(),end=threads.end();
+ it!=end;
+ ++it)
+ {
+ if ((*it)->get_id() == id)
+ return true;
+ }
+ return false;
+ }
+ else
+ {
+ return false;
+ }
+ }
+
         template<typename F>
         thread* create_thread(F threadfunc)
         {
@@ -49,6 +84,10 @@
         {
             if(thrd)
             {
+ BOOST_THREAD_ASSERT_PRECONDITION( ! is_thread_in(thrd) ,
+ thread_resource_error(system::errc::resource_deadlock_would_occur, "boost::thread_group: trying to add a duplicated thread")
+ );
+
                 boost::lock_guard<shared_mutex> guard(m);
                 threads.push_back(thrd);
             }
@@ -66,6 +105,9 @@
 
         void join_all()
         {
+ BOOST_THREAD_ASSERT_PRECONDITION( ! is_this_thread_in() ,
+ thread_resource_error(system::errc::resource_deadlock_would_occur, "boost::thread_group: trying joining itself")
+ );
             boost::shared_lock<shared_mutex> guard(m);
 
             for(std::list<thread*>::iterator it=threads.begin(),end=threads.end();

Modified: trunk/libs/thread/doc/thread_ref.qbk
==============================================================================
--- trunk/libs/thread/doc/thread_ref.qbk (original)
+++ trunk/libs/thread/doc/thread_ref.qbk 2012-11-10 19:57:07 EST (Sat, 10 Nov 2012)
@@ -761,9 +761,9 @@
 
 [[Error Conditions:] [
 
-[*resource_deadlock_would_occur]: if deadlock is detected or this->get_id() == boost::this_thread::get_id().
+[*resource_deadlock_would_occur]: if deadlock is detected or `this->get_id() == boost::this_thread::get_id()` and `BOOST_THREAD_TRHOW_IF_PRECONDITION_NOT_SATISFIED` is defined..
 
-[*invalid_argument]: if the thread is not joinable and BOOST_THREAD_TRROW_IF_PRECONDITION_NOT_SATISFIED is defined.
+[*invalid_argument]: if the thread is not joinable and `BOOST_THREAD_TRHOW_IF_PRECONDITION_NOT_SATISFIED` is defined.
 
 
 [/
@@ -814,7 +814,7 @@
 
 [*resource_deadlock_would_occur]: if deadlock is detected or this->get_id() == boost::this_thread::get_id().
 
-[*invalid_argument]: if the thread is not joinable and BOOST_THREAD_TRROW_IF_PRECONDITION_NOT_SATISFIED is defined.
+[*invalid_argument]: if the thread is not joinable and BOOST_THREAD_TRHOW_IF_PRECONDITION_NOT_SATISFIED is defined.
 
 
 [/
@@ -854,7 +854,7 @@
 
 [*resource_deadlock_would_occur]: if deadlock is detected or this->get_id() == boost::this_thread::get_id().
 
-[*invalid_argument]: if the thread is not joinable and BOOST_THREAD_TRROW_IF_PRECONDITION_NOT_SATISFIED is defined.
+[*invalid_argument]: if the thread is not joinable and BOOST_THREAD_TRHOW_IF_PRECONDITION_NOT_SATISFIED is defined.
 
 
 [/
@@ -894,7 +894,7 @@
 
 [*resource_deadlock_would_occur]: if deadlock is detected or this->get_id() == boost::this_thread::get_id().
 
-[*invalid_argument]: if the thread is not joinable and BOOST_THREAD_TRROW_IF_PRECONDITION_NOT_SATISFIED is defined.
+[*invalid_argument]: if the thread is not joinable and BOOST_THREAD_TRHOW_IF_PRECONDITION_NOT_SATISFIED is defined.
 
 
 [/
@@ -930,7 +930,7 @@
 
 [*no_such_process]: if the thread is not valid.
 
-[*invalid_argument]: if the thread is not joinable and BOOST_THREAD_TRROW_IF_PRECONDITION_NOT_SATISFIED is defined.
+[*invalid_argument]: if the thread is not joinable and BOOST_THREAD_TRHOW_IF_PRECONDITION_NOT_SATISFIED is defined.
 
 ]]
 
@@ -1719,6 +1719,8 @@
         thread* create_thread(F threadfunc);
         void add_thread(thread* thrd);
         void remove_thread(thread* thrd);
+ bool is_this_thread_in();
+ bool is_thread_in(thread* thrd);
         void join_all();
         void interrupt_all();
         int size() const;
@@ -1774,7 +1776,7 @@
 
 [variablelist
 
-[[Precondition:] [The expression `delete thrd` is well-formed and will not result in undefined behaviour.]]
+[[Precondition:] [The expression `delete thrd` is well-formed and will not result in undefined behaviour and `is_thread_in(thrd) == false`.]]
 
 [[Effects:] [Take ownership of the __thread__ object pointed to by `thrd` and add it to the group.]]
 
@@ -1804,6 +1806,8 @@
 
 [variablelist
 
+[[Requires:] [`is_this_thread_in() == false`.]]
+
 [[Effects:] [Call `join()` on each __thread__ object in the group.]]
 
 [[Postcondition:] [Every thread in the group has terminated.]]
@@ -1814,6 +1818,32 @@
 
 [endsect]
 
+[section:is_this_thread_in Member function `is_this_thread_in()`]
+
+ bool is_this_thread_in();
+
+[variablelist
+
+[[Returns:] [true if there is a thread `th` in the group such that `th.get_id() == this_thread::get_id()`.]]
+
+
+]
+
+[endsect]
+
+[section:is_thread_in Member function `is_thread_in()`]
+
+ bool is_thread_in(thread* thrd);
+
+[variablelist
+
+[[Returns:] [true if there is a thread `th` in the group such that `th.get_id() == thrd->get_id()`.]]
+
+
+]
+
+[endsect]
+
 [section:interrupt_all Member function `interrupt_all()`]
 
     void interrupt_all();

Modified: trunk/libs/thread/example/thread_group.cpp
==============================================================================
--- trunk/libs/thread/example/thread_group.cpp (original)
+++ trunk/libs/thread/example/thread_group.cpp 2012-11-10 19:57:07 EST (Sat, 10 Nov 2012)
@@ -6,6 +6,7 @@
 
 #include <boost/thread/thread.hpp>
 #include <iostream>
+#include <boost/detail/lightweight_test.hpp>
 
 int count = 0;
 boost::mutex mutex;
@@ -16,10 +17,54 @@
     std::cout << "count = " << ++count << std::endl;
 }
 
+boost::thread_group threads2;
+boost::thread* th2 = 0;
+
+void increment_count_2()
+{
+ boost::unique_lock<boost::mutex> lock(mutex);
+ BOOST_TEST(threads2.is_this_thread_in());
+ std::cout << "count = " << ++count << std::endl;
+}
+
 int main()
 {
+ {
     boost::thread_group threads;
- for (int i = 0; i < 10; ++i)
+ for (int i = 0; i < 3; ++i)
         threads.create_thread(&increment_count);
     threads.join_all();
+ }
+ {
+ boost::thread_group threads;
+ for (int i = 0; i < 3; ++i)
+ threads.create_thread(&increment_count);
+ threads.interrupt_all();
+ threads.join_all();
+ }
+ {
+ boost::thread_group threads;
+ boost::thread* th = new boost::thread(&increment_count);
+ threads.add_thread(th);
+ BOOST_TEST(! threads.is_this_thread_in());
+ threads.join_all();
+ }
+ {
+ boost::thread_group threads;
+ boost::thread* th = new boost::thread(&increment_count);
+ threads.add_thread(th);
+ BOOST_TEST(threads.is_thread_in(th));
+ threads.remove_thread(th);
+ BOOST_TEST(! threads.is_thread_in(th));
+ th->join();
+ }
+ {
+ {
+ boost::unique_lock<boost::mutex> lock(mutex);
+ boost::thread* th2 = new boost::thread(&increment_count_2);
+ threads2.add_thread(th2);
+ }
+ threads2.join_all();
+ }
+ return boost::report_errors();
 }


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk