Commit 7ba8ea9c authored by Davis King's avatar Davis King

Made dlib::thread_pool use std::thread and join on the threads in

thread_pool's destructor.  The previous implementation used dlib's global
thread pooling to allocate threads to dlib::thread_pool, however, this
sometimes caused annoying behavior when used as part of a MATLAB mex file.
parent cedccc9f
......@@ -19,12 +19,11 @@ namespace dlib
we_are_destructing(false)
{
tasks.resize(num_threads);
threads.resize(num_threads);
for (unsigned long i = 0; i < num_threads; ++i)
{
register_thread(*this, &thread_pool_implementation::thread);
threads[i] = std::thread([&](){this->thread();});
}
start();
}
// ----------------------------------------------------------------------------------------
......@@ -60,7 +59,10 @@ namespace dlib
task_ready_signaler.broadcast();
}
wait();
// wait for all threads to terminate
for (auto& t : threads)
t.join();
threads.clear();
// Throw any unhandled exceptions. Since shutdown_pool() is only called in the
// destructor this will kill the program.
......
......@@ -14,6 +14,7 @@
#include "../smart_pointers_thread_safe.h"
#include "../smart_pointers.h"
#include <exception>
#include <thread>
namespace dlib
{
......@@ -126,7 +127,7 @@ namespace dlib
// ----------------------------------------------------------------------------------------
class thread_pool_implementation : private multithreaded_object
class thread_pool_implementation
{
/*!
CONVENTION
......@@ -474,6 +475,8 @@ namespace dlib
signaler task_ready_signaler;
bool we_are_destructing;
std::vector<std::thread> threads;
// restricted functions
thread_pool_implementation(thread_pool_implementation&); // copy constructor
thread_pool_implementation& operator=(thread_pool_implementation&); // assignment operator
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment