From f90a5705bd65a4ebb5edc9df003a383039fec555 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 29 Apr 2018 08:17:30 +0200 Subject: Change design, fix crash when closing application --- src/Cache.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'src/Cache.cpp') diff --git a/src/Cache.cpp b/src/Cache.cpp index bfa3df4..d402d36 100644 --- a/src/Cache.cpp +++ b/src/Cache.cpp @@ -95,11 +95,12 @@ namespace dchat return { (sf::Texture*)nullptr, ImageByUrlResult::Type::FAILED_DOWNLOAD }; } - Cache::Cache() + Cache::Cache() : + alive(true) { downloadWaitThread = thread([this] { - while(true) + while(alive) { for(vector::iterator it = imageDownloadProcesses.begin(); it != imageDownloadProcesses.end();) { @@ -133,7 +134,7 @@ namespace dchat ++it; } - while(imageDownloadProcesses.empty() && imageDownloadProcessesQueue.empty()) + while(alive && imageDownloadProcesses.empty() && imageDownloadProcessesQueue.empty()) this_thread::sleep_for(chrono::milliseconds(20)); if(!imageDownloadProcessesQueue.empty()) @@ -150,7 +151,12 @@ namespace dchat this_thread::sleep_for(chrono::milliseconds(20)); } }); - downloadWaitThread.detach(); + } + + Cache::~Cache() + { + alive = false; + downloadWaitThread.join(); } const ImageByUrlResult Cache::getImageByUrl(const string &url, int downloadLimitBytes) -- cgit v1.2.3