Don't refer this pointer inside the detached thread
Signed-off-by: Tao He <sighingnow@gmail.com>
This commit is contained in:
parent
f774f832de
commit
80b4d2178f
|
|
@ -298,10 +298,12 @@ void etcd::Watcher::doWatch(std::string const & key,
|
|||
stubs->call->waitForResponse(callback);
|
||||
if (wait_callback != nullptr) {
|
||||
// issue the callback in another thread (detached) to avoid deadlock,
|
||||
// it is ok to detach a pplx::task, but we cannot use the pplx::task
|
||||
// it is ok to detach a pplx::task, but we don't want to use the pplx::task
|
||||
// in the core library
|
||||
std::thread canceller([this]() {
|
||||
wait_callback(stubs->call->Cancelled());
|
||||
bool cancelled = stubs->call->Cancelled();
|
||||
std::function<void(bool)> wait_callback = this->wait_callback;
|
||||
std::thread canceller([wait_callback, cancelled]() {
|
||||
wait_callback(cancelled);
|
||||
});
|
||||
canceller.detach();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
#include "etcd/SyncClient.hpp"
|
||||
#include "etcd/Watcher.hpp"
|
||||
|
||||
static const std::string etcd_url("http://127.0.0.1:2379");
|
||||
static const std::string etcd_url("http://127.0.0.1:24799");
|
||||
|
||||
static int watcher_called = 0;
|
||||
|
||||
|
|
@ -33,9 +33,17 @@ void print_response(etcd::Response const & resp)
|
|||
}
|
||||
}
|
||||
|
||||
void wait_for_connection(etcd::Client &client) {
|
||||
void wait_for_connection(std::string endpoints) {
|
||||
// wait until the client connects to etcd server
|
||||
while (!client.head().get().is_ok()) {
|
||||
while (true) {
|
||||
try {
|
||||
etcd::Client client(endpoints);
|
||||
if (client.head().get().is_ok()) {
|
||||
break;
|
||||
}
|
||||
} catch (...) {
|
||||
// pass
|
||||
}
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
|
|
@ -44,8 +52,9 @@ void initialize_watcher(const std::string& endpoints,
|
|||
const std::string& prefix,
|
||||
std::function<void(etcd::Response)> callback,
|
||||
std::shared_ptr<etcd::Watcher>& watcher) {
|
||||
// wait until the endpoints turn to be available
|
||||
wait_for_connection(endpoints);
|
||||
etcd::Client client(endpoints);
|
||||
wait_for_connection(client);
|
||||
|
||||
// Check if the failed one has been cancelled first
|
||||
if (watcher && watcher->Cancelled()) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue