threadpool: catch exceptions in dtor, to avoid terminate

If an exception is thrown, it is ignored. While this may hide
a bug, this should only be system exceptions in boost, which
is pretty unlikely. Morever, wait should be called manually
before the dtor anyway. Add an error message if the dtor has
to wait in case some such cases creep in so they get fixed.

Coverity 182538
This commit is contained in:
moneromooo-monero 2018-02-02 16:16:43 +00:00
parent 24803ed91f
commit 9b98a6ac8f
No known key found for this signature in database
GPG Key ID: 686F07454D6CEFC3
2 changed files with 20 additions and 1 deletions

View File

@ -25,6 +25,7 @@
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "misc_log_ex.h"
#include "common/threadpool.h" #include "common/threadpool.h"
#include <cassert> #include <cassert>
@ -81,6 +82,23 @@ int threadpool::get_max_concurrency() {
return max; return max;
} }
threadpool::waiter::~waiter()
{
{
boost::unique_lock<boost::mutex> lock(mt);
if (num)
MERROR("wait should have been called before waiter dtor - waiting now");
}
try
{
wait();
}
catch (const std::exception &e)
{
/* ignored */
}
}
void threadpool::waiter::wait() { void threadpool::waiter::wait() {
boost::unique_lock<boost::mutex> lock(mt); boost::unique_lock<boost::mutex> lock(mt);
while(num) cv.wait(lock); while(num) cv.wait(lock);

View File

@ -34,6 +34,7 @@
#include <functional> #include <functional>
#include <utility> #include <utility>
#include <vector> #include <vector>
#include <stdexcept>
namespace tools namespace tools
{ {
@ -57,7 +58,7 @@ public:
void dec(); void dec();
void wait(); //! Wait for a set of tasks to finish. void wait(); //! Wait for a set of tasks to finish.
waiter() : num(0){} waiter() : num(0){}
~waiter() { wait(); } ~waiter();
}; };
// Submit a task to the pool. The waiter pointer may be // Submit a task to the pool. The waiter pointer may be