diff options
Diffstat (limited to 'src/AsyncImageLoader.cpp')
-rw-r--r-- | src/AsyncImageLoader.cpp | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/src/AsyncImageLoader.cpp b/src/AsyncImageLoader.cpp index 2f0f869..05c6a7e 100644 --- a/src/AsyncImageLoader.cpp +++ b/src/AsyncImageLoader.cpp @@ -103,7 +103,7 @@ namespace QuickMedia { } AsyncImageLoader::AsyncImageLoader() { - for(size_t i = 0; i < NUM_IMAGE_LOAD_THREADS; ++i) { + for(int i = 0; i < NUM_IMAGE_LOAD_THREADS; ++i) { loading_image[i] = false; } @@ -147,6 +147,12 @@ namespace QuickMedia { load_image_cv.notify_one(); } load_image_thread.join(); + + // TODO: Find a way to kill the threads instead. We need to do this right now because creating a new thread before the last one has died causes a crash + for(size_t i = 0; i < NUM_IMAGE_LOAD_THREADS; ++i) { + if(download_image_thread[i].joinable()) + download_image_thread[i].join(); + } } void AsyncImageLoader::load_thumbnail(const std::string &url, bool local, sf::Vector2i resize_target_size, bool use_tor, std::shared_ptr<ThumbnailData> thumbnail_data) { @@ -182,6 +188,8 @@ namespace QuickMedia { loading_image[free_index] = true; thumbnail_data->loading_state = LoadingState::LOADING; + if(download_image_thread[free_index].joinable()) + download_image_thread[free_index].join(); // TODO: Keep the thread running and use conditional variable instead to sleep until a new image should be loaded. Same in ImageViewer. download_image_thread[free_index] = std::thread([this, free_index, thumbnail_path, url, local, resize_target_size, thumbnail_data, use_tor]() mutable { @@ -199,9 +207,8 @@ namespace QuickMedia { return; } } else { - Path download_path = thumbnail_path; - download_path.append(".orig"); - if(download_to_file(url, download_path.data, {}, use_tor, true) != DownloadResult::OK || !thumbnail_data->image->loadFromFile(download_path.data)) { + // Use the same path as the thumbnail path, since it wont be overwritten if the image is smaller than the thumbnail target size + if(download_to_file(url, thumbnail_path.data, {}, use_tor, true) != DownloadResult::OK || !thumbnail_data->image->loadFromFile(thumbnail_path.data)) { thumbnail_data->loading_state = LoadingState::FINISHED_LOADING; loading_image[free_index] = false; return; @@ -216,7 +223,6 @@ namespace QuickMedia { loading_image[free_index] = false; return; }); - download_image_thread[free_index].detach(); } int AsyncImageLoader::get_free_load_index() const { |