DiscoveryClient::~DiscoveryClient() { if (_registered.load(butil::memory_order_acquire)) { bthread_stop(_th); bthread_join(_th, NULL); DoCancel(); } }
TEST_F(BthreadTest, stop_sleep) { bthread_t th; ASSERT_EQ(0, bthread_start_urgent( &th, NULL, sleep_for_awhile_with_sleep, (void*)1000000L)); butil::Timer tm; tm.start(); bthread_usleep(10000); ASSERT_EQ(0, bthread_stop(th)); ASSERT_EQ(0, bthread_join(th, NULL)); tm.stop(); ASSERT_LE(labs(tm.m_elapsed() - 10), 5); }
NamingServiceThread::~NamingServiceThread() { RPC_VLOG << "~NamingServiceThread(" << *this << ')'; // Remove from g_nsthread_map first if (_source_ns != NULL) { const NSKey key = { _source_ns, _service_name }; std::unique_lock<pthread_mutex_t> mu(g_nsthread_map_mutex); if (g_nsthread_map != NULL) { NamingServiceThread** ptr = g_nsthread_map->seek(key); if (ptr != NULL && *ptr == this) { g_nsthread_map->erase(key); } } } if (_tid) { bthread_stop(_tid); bthread_join(_tid, NULL); _tid = 0; } { BAIDU_SCOPED_LOCK(_mutex); std::vector<ServerId> to_be_removed; ServerNodeWithId2ServerId(_last_sockets, &to_be_removed, NULL); if (!_last_sockets.empty()) { for (std::map<NamingServiceWatcher*, const NamingServiceFilter*>::iterator it = _watchers.begin(); it != _watchers.end(); ++it) { it->first->OnRemovedServers(to_be_removed); } } _watchers.clear(); } if (_ns) { _ns->Destroy(); _ns = NULL; } }