ImageLoader detach

This commit is contained in:
hemirt 2017-10-01 00:48:24 +02:00
parent f89761a9e3
commit c22fcd0c5e
2 changed files with 5 additions and 14 deletions

View file

@ -18,7 +18,7 @@ namespace messages {
ImageLoader::ImageLoader() ImageLoader::ImageLoader()
{ {
this->worker = std::thread(&ImageLoader::run, this); std::thread(&ImageLoader::run, this).detach();
} }
ImageLoader::~ImageLoader() ImageLoader::~ImageLoader()
@ -29,14 +29,10 @@ ImageLoader::~ImageLoader()
this->ready = true; this->ready = true;
} }
this->cv.notify_all(); this->cv.notify_all();
qDebug() << "notified waiting";
this->worker.join();
qDebug() << "destruct";
} }
void ImageLoader::run() void ImageLoader::run()
{ {
this->eventLoop.reset(new QEventLoop());
while (true) { while (true) {
std::unique_lock<std::mutex> lk(this->workerM); std::unique_lock<std::mutex> lk(this->workerM);
if (!this->ready) { if (!this->ready) {
@ -52,17 +48,14 @@ void ImageLoader::run()
this->ready = false; this->ready = false;
lk.unlock(); lk.unlock();
for (auto &lli : current) { for (auto &lli : current) {
if (this->exit)
return;
QNetworkRequest request; QNetworkRequest request;
request.setUrl(QUrl(lli->url)); request.setUrl(QUrl(lli->url));
QNetworkAccessManager NaM; QNetworkAccessManager NaM;
QNetworkReply *reply = NaM.get(request); QNetworkReply *reply = NaM.get(request);
QObject::connect(reply, &QNetworkReply::finished, this->eventLoop.get(), QEventLoop eventLoop;
&QEventLoop::quit); QObject::connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
qDebug() << "eve1"; // Wait until response is read.
this->eventLoop->exec(); // Wait until response is read. eventLoop.exec();
qDebug() << "eve2";
qDebug() << "Received emote " << lli->url; qDebug() << "Received emote " << lli->url;
QByteArray array = reply->readAll(); QByteArray array = reply->readAll();
QBuffer buffer(&array); QBuffer buffer(&array);

View file

@ -22,13 +22,11 @@ public:
private: private:
void run(); void run();
std::thread worker;
std::mutex workerM; std::mutex workerM;
std::vector<chatterino::messages::LazyLoadedImage *> toProcess; std::vector<chatterino::messages::LazyLoadedImage *> toProcess;
bool ready = false; bool ready = false;
std::condition_variable cv; std::condition_variable cv;
bool exit = false; bool exit = false;
std::unique_ptr<QEventLoop> eventLoop;
}; };
} // namespace messages } // namespace messages