2
0
mirror of https://github.com/boostorg/thread.git synced 2026-01-24 06:22:12 +00:00

Fix task exception silently ignored. Make submit throw if closed. join the threads created by the thread_executor

This commit is contained in:
Vicente J. Botet Escriba
2014-11-02 15:09:11 +01:00
parent cbf9fe8a5c
commit c1925df81c
5 changed files with 81 additions and 34 deletions

View File

@@ -48,9 +48,9 @@ namespace executors
*/
bool try_executing_one()
{
work task;
try
{
work task;
if (work_queue.try_pull_front(task) == queue_op_status::success)
{
task();
@@ -58,12 +58,9 @@ namespace executors
}
return false;
}
catch (std::exception& )
{
return false;
}
catch (...)
{
std::terminate();
return false;
}
}
@@ -95,12 +92,9 @@ namespace executors
task();
}
}
catch (std::exception& )
{
return;
}
catch (...)
{
std::terminate();
return;
}
}

View File

@@ -26,6 +26,7 @@ namespace executors
/// type-erasure to store the works to do
typedef executors::work work;
bool closed_;
mutable mutex mtx_;
/**
* Effects: try to execute one task.
* Returns: whether a task has been executed.
@@ -66,16 +67,22 @@ namespace executors
*/
void close()
{
lock_guard<mutex> lk(mtx_);
closed_ = true;
}
/**
* \b Returns: whether the pool is closed for submissions.
*/
bool closed()
bool closed(lock_guard<mutex>& )
{
return closed_;
}
bool closed()
{
lock_guard<mutex> lk(mtx_);
return closed(lk);
}
/**
* \b Requires: \c Closure is a model of \c Callable(void()) and a model of \c CopyConstructible/MoveConstructible.
@@ -93,21 +100,48 @@ namespace executors
template <typename Closure>
void submit(Closure & closure)
{
if (closed()) return;
closure();
lock_guard<mutex> lk(mtx_);
if (closed(lk)) BOOST_THROW_EXCEPTION( sync_queue_is_closed() );
try
{
closure();
}
catch (...)
{
std::terminate();
return;
}
}
#endif
void submit(void (*closure)())
{
if (closed()) return;
closure();
lock_guard<mutex> lk(mtx_);
if (closed(lk)) BOOST_THROW_EXCEPTION( sync_queue_is_closed() );
try
{
closure();
}
catch (...)
{
std::terminate();
return;
}
}
template <typename Closure>
void submit(BOOST_THREAD_FWD_REF(Closure) closure)
{
if (closed()) return;
closure();
lock_guard<mutex> lk(mtx_);
if (closed(lk)) BOOST_THROW_EXCEPTION( sync_queue_is_closed() );
try
{
closure();
}
catch (...)
{
std::terminate();
return;
}
}
/**

View File

@@ -51,12 +51,9 @@ namespace executors
}
return false;
}
catch (std::exception& )
{
return false;
}
catch (...)
{
std::terminate();
return false;
}
}

View File

@@ -43,7 +43,15 @@ namespace executors
try_executing_one_task(work& task, boost::promise<void> &p)
: task(task), p(p) {}
void operator()() {
task(); // if task() throws promise is not set but as the the program terminates and should terminate there is no need to use try-catch here.
try
{
task();
}
catch (...)
{
std::terminate();
return;
}
p.set_value();
}
};
@@ -52,7 +60,7 @@ namespace executors
* \par Returns
* The underlying executor wrapped on a generic executor reference.
*/
generic_executor_ref underlying_executor() BOOST_NOEXCEPT { return ex; }
generic_executor_ref& underlying_executor() BOOST_NOEXCEPT { return ex; }
/**
* Effects: try to execute one task.
@@ -79,12 +87,9 @@ namespace executors
}
return false;
}
catch (std::exception& )
{
return false;
}
catch (...)
{
std::terminate();
return false;
}
}

View File

@@ -15,6 +15,8 @@
#include <boost/thread/executors/work.hpp>
#include <boost/thread/executors/executor.hpp>
#include <boost/thread/thread_only.hpp>
#include <boost/thread/scoped_thread.hpp>
#include <boost/thread/csbl/vector.hpp>
#include <boost/config/abi_prefix.hpp>
@@ -28,6 +30,11 @@ namespace executors
/// type-erasure to store the works to do
typedef executors::work work;
bool closed_;
typedef scoped_thread<> thread_t;
typedef csbl::vector<thread_t> threads_type;
threads_type threads_;
mutable mutex mtx_;
/**
* Effects: try to execute one task.
* Returns: whether a task has been executed.
@@ -52,7 +59,7 @@ namespace executors
{
}
/**
* \b Effects: Destroys the inline executor.
* \b Effects: Waits for closures (if any) to complete, then joins and destroys the threads.
*
* \b Synchronization: The completion of all the closures happen before the completion of the \c thread_executor destructor.
*/
@@ -60,6 +67,7 @@ namespace executors
{
// signal to all the worker thread that there will be no more submissions.
close();
// all the scoped threads will join before destroying
}
/**
@@ -68,16 +76,22 @@ namespace executors
*/
void close()
{
lock_guard<mutex> lk(mtx_);
closed_ = true;
}
/**
* \b Returns: whether the pool is closed for submissions.
*/
bool closed()
bool closed(lock_guard<mutex>& )
{
return closed_;
}
bool closed()
{
lock_guard<mutex> lk(mtx_);
return closed(lk);
}
/**
* \b Requires: \c Closure is a model of \c Callable(void()) and a model of \c CopyConstructible/MoveConstructible.
@@ -95,24 +109,27 @@ namespace executors
template <typename Closure>
void submit(Closure & closure)
{
if (closed()) return;
lock_guard<mutex> lk(mtx_);
if (closed(lk)) BOOST_THROW_EXCEPTION( sync_queue_is_closed() );
thread th(closure);
th.detach();
threads_.push_back(thread_t(boost::move(th)));
}
#endif
void submit(void (*closure)())
{
if (closed()) return;
lock_guard<mutex> lk(mtx_);
if (closed(lk)) BOOST_THROW_EXCEPTION( sync_queue_is_closed() );
thread th(closure);
th.detach();
threads_.push_back(thread_t(boost::move(th)));
}
template <typename Closure>
void submit(BOOST_THREAD_FWD_REF(Closure) closure)
{
if (closed()) return;
lock_guard<mutex> lk(mtx_);
if (closed(lk)) BOOST_THROW_EXCEPTION( sync_queue_is_closed() );
thread th(boost::forward<Closure>(closure));
th.detach();
threads_.push_back(thread_t(boost::move(th)));
}
/**