Explicitly stop the RPCWorker in RPCServer destructor

This commit is contained in:
Scott Kyle 2016-05-03 12:05:58 -07:00
parent 32219cf0a3
commit 18a4d66903
2 changed files with 11 additions and 2 deletions

View File

@ -59,8 +59,7 @@ RPCWorker::RPCWorker() {
} }
RPCWorker::~RPCWorker() { RPCWorker::~RPCWorker() {
m_stop = true; stop();
m_thread.join();
} }
void RPCWorker::add_task(std::function<json()> task) { void RPCWorker::add_task(std::function<json()> task) {
@ -89,6 +88,13 @@ void RPCWorker::try_run_task() {
} }
} }
void RPCWorker::stop() {
if (!m_stop) {
m_stop = true;
m_thread.join();
}
}
RPCServer::RPCServer() { RPCServer::RPCServer() {
m_context = JSGlobalContextCreate(NULL); m_context = JSGlobalContextCreate(NULL);
get_rpc_server(m_context) = this; get_rpc_server(m_context) = this;
@ -190,6 +196,8 @@ RPCServer::RPCServer() {
} }
RPCServer::~RPCServer() { RPCServer::~RPCServer() {
m_worker.stop();
// The protected values should be unprotected before releasing the context. // The protected values should be unprotected before releasing the context.
m_objects.clear(); m_objects.clear();
m_callbacks.clear(); m_callbacks.clear();

View File

@ -46,6 +46,7 @@ class RPCWorker {
void add_task(std::function<json()>); void add_task(std::function<json()>);
json pop_task_result(); json pop_task_result();
void try_run_task(); void try_run_task();
void stop();
private: private:
bool m_stop = false; bool m_stop = false;