1
0
mirror of https://github.com/asamy/ctorrent synced 2025-10-05 23:52:41 +02:00

fix scheduler not exiting properly

Signed-off-by: Ahmed Samy <f.fallen45@gmail.com>
This commit is contained in:
Ahmed Samy
2016-08-22 01:05:18 +00:00
parent a59cf440aa
commit 9c92d6aa32
3 changed files with 14 additions and 8 deletions

View File

@@ -78,8 +78,9 @@ public:
}
~TorrentFileManagerImpl() {
lock();
m_stopped = true;
m_condition.notify_all();
unlock_and_notify();
m_thread.join();
}
@@ -289,6 +290,9 @@ void TorrentFileManagerImpl::thread()
m_readRequests.pop();
}
if (m_stopped)
break;
lock.unlock();
}
}

View File

@@ -178,7 +178,7 @@ int main(int argc, char *argv[])
size_t eseed = 0;
size_t started = 0;
Torrent *torrents = new Torrent[total]; // Workaround for CLang non-POD
std::vector<Torrent> torrents(total);
for (size_t i = 0; i < total; ++i) {
std::string file = files[i];
Torrent *t = &torrents[i];
@@ -251,7 +251,7 @@ int main(int argc, char *argv[])
}
Connection::poll();
print_all_stats(torrents, total);
print_all_stats(&torrents[0], total);
std::this_thread::sleep_for(std::chrono::milliseconds(5));
}
}
@@ -272,7 +272,7 @@ int main(int argc, char *argv[])
}
Connection::poll();
print_all_stats(torrents, total);
print_all_stats(&torrents[0], total);
std::this_thread::sleep_for(std::chrono::milliseconds(5));
}
}
@@ -293,7 +293,6 @@ int main(int argc, char *argv[])
std::clog << meta->name() << std::endl;
}
delete []torrents;
logfile.close();
return 0;
}

View File

@@ -84,11 +84,14 @@ public:
}
void stop() {
mutex.lock();
m_stopped = true;
m_condition.notify_one();
mutex.unlock();
m_condition.notify_all();
m_thread.join();
m_dispatcherThread.join();
}
void notify_one() { m_condition.notify_one(); }
void notify_all() { m_condition.notify_all(); }
protected:
@@ -122,7 +125,7 @@ uint32_t Scheduler::addEvent(const SchedulerCallback &cb, uint32_t ms)
i->events.push(ev);
i->mutex.unlock();
if (notify)
i->notify_all();
i->notify_one();
return ev.id();
}
@@ -174,7 +177,7 @@ void SchedulerImpl::thread()
// Ugly hack for newly added events
fart:
bool cont = m_condition.wait_until(m, std::chrono::system_clock::now()
+ std::chrono::milliseconds(5), [this] () { return events.top().expired(); });
+ std::chrono::milliseconds(5), [this] () { return events.top().expired() || m_stopped; });
if (m_stopped)
break;
else if (!cont)