问题
I am trying to learn std::threads
from C++11
to make a threading system.
I was wondering if there is a way to stop a thread from running (Not sleeping, but really destructing the thread or so to speak) without terminating the whole program.
I know std::join
exists, but that forces a thread to wait till all threads return.
Is there another way to handle this? (For example for making a ThreadPool
class without having to block a thread?)
回答1:
The C++ std::thread
class is really just a minimal interface layered on top of some more complete implementation-defined threading package. As such, it only defines a tiny amount of functionality -- creating new threads, detach
, and join
. That's pretty much it -- there's no standard way of managing, scheduling, stopping/starting/killing threads or doing much else.
There is a native_handle
method that returns an implementation-defined type which can probably be used to do what you want, depending on the implementation.
回答2:
Maybe this thread_pool will help you:
#include <boost/thread.hpp>
#include <boost/phoenix.hpp>
#include <boost/optional.hpp>
using namespace boost;
using namespace boost::phoenix::arg_names;
boost::atomic_size_t counter(0ul);
class thread_pool
{
private:
mutex mx;
condition_variable cv;
typedef function<void()> job_t;
std::deque<job_t> _queue;
thread_group pool;
boost::atomic_bool shutdown;
static void worker_thread(thread_pool& q)
{
while (optional<job_t> job = q.dequeue())
(*job)();
}
public:
thread_pool() : shutdown(false) {
for (unsigned i = 0; i < boost::thread::hardware_concurrency(); ++i)
pool.create_thread(bind(worker_thread, ref(*this)));
}
void enqueue(job_t job)
{
lock_guard<mutex> lk(mx);
_queue.push_back(job);
cv.notify_one();
}
optional<job_t> dequeue()
{
unique_lock<mutex> lk(mx);
namespace phx = boost::phoenix;
cv.wait(lk, phx::ref(shutdown) || !phx::empty(phx::ref(_queue)));
if (_queue.empty())
return none;
job_t job = _queue.front();
_queue.pop_front();
return job;
}
~thread_pool()
{
shutdown = true;
{
lock_guard<mutex> lk(mx);
cv.notify_all();
}
pool.join_all();
}
};
Example of use: live On Coliru
回答3:
There is no way to non-cooperatively stop a thread from running with standard C++. That doesn't mean it's impossible, but you may need to go back to your systems native handle.
For a standard conforming way you can use synchronization primitives (e.g. an std::atomic<bool>
) to set a kill flag from the outside and read it within the thread. But it still has to be the thread that finishes on its own.
来源:https://stackoverflow.com/questions/23117354/how-to-stop-an-stdthread-from-running-without-terminating-the-program