Skip to content

Commit a4feb59

Browse files
committed
Fixed hang in release mode due to missing lock of empty function in thread. Replaced busy waiting with a condition variable.
1 parent d4c5c04 commit a4feb59

2 files changed

Lines changed: 16 additions & 15 deletions

File tree

ImGuiFileDialog.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2725,7 +2725,10 @@ void IGFD::ThumbnailFeature::m_StartThumbnailFileDatasExtraction() {
27252725
m_CountFiles = 0U;
27262726
m_ThumbnailGenerationThread = std::shared_ptr<std::thread>(new std::thread(&IGFD::ThumbnailFeature::m_ThreadThumbnailFileDatasExtractionFunc, this), [this](std::thread* obj) {
27272727
m_IsWorking = false;
2728-
if (obj) obj->join();
2728+
if (obj) {
2729+
m_ThumbnailFileDatasToGetCv.notify_all();
2730+
obj->join();
2731+
}
27292732
});
27302733
}
27312734
}
@@ -2745,12 +2748,14 @@ void IGFD::ThumbnailFeature::m_ThreadThumbnailFileDatasExtractionFunc() {
27452748

27462749
// infinite loop while is thread working
27472750
while (m_IsWorking) {
2751+
std::unique_lock<std::mutex> thumbnailFileDatasToGetLock(m_ThumbnailFileDatasToGetMutex);
2752+
m_ThumbnailFileDatasToGetCv.wait(thumbnailFileDatasToGetLock);
27482753
if (!m_ThumbnailFileDatasToGet.empty()) {
27492754
std::shared_ptr<FileInfos> file = nullptr;
2750-
m_ThumbnailFileDatasToGetMutex.lock();
27512755
// get the first file in the list
27522756
file = (*m_ThumbnailFileDatasToGet.begin());
2753-
m_ThumbnailFileDatasToGetMutex.unlock();
2757+
m_ThumbnailFileDatasToGet.pop_front();
2758+
thumbnailFileDatasToGetLock.unlock();
27542759

27552760
// retrieve datas of the texture file if its an image file
27562761
if (file.use_count()) {
@@ -2803,16 +2808,9 @@ void IGFD::ThumbnailFeature::m_ThreadThumbnailFileDatasExtractionFunc() {
28032808
}
28042809
}
28052810
}
2806-
2807-
// peu importe le resultat on vire le fichier
2808-
// remove form this list
2809-
// write => thread concurency issues
2810-
m_ThumbnailFileDatasToGetMutex.lock();
2811-
m_ThumbnailFileDatasToGet.pop_front();
2812-
m_ThumbnailFileDatasToGetMutex.unlock();
28132811
}
28142812
} else {
2815-
std::this_thread::sleep_for(std::chrono::milliseconds(100));
2813+
thumbnailFileDatasToGetLock.unlock();
28162814
}
28172815
}
28182816
}
@@ -2849,6 +2847,7 @@ void IGFD::ThumbnailFeature::m_AddThumbnailToLoad(const std::shared_ptr<FileInfo
28492847
m_ThumbnailFileDatasToGet.push_back(vFileInfos);
28502848
vFileInfos->thumbnailInfo.isLoadingOrLoaded = true;
28512849
m_ThumbnailFileDatasToGetMutex.unlock();
2850+
m_ThumbnailFileDatasToGetCv.notify_all();
28522851
}
28532852
}
28542853
}
@@ -2913,31 +2912,31 @@ void IGFD::ThumbnailFeature::SetDestroyThumbnailCallback(const DestroyThumbnailF
29132912

29142913
void IGFD::ThumbnailFeature::ManageGPUThumbnails() {
29152914
if (m_CreateThumbnailFun) {
2915+
m_ThumbnailToCreateMutex.lock();
29162916
if (!m_ThumbnailToCreate.empty()) {
29172917
for (const auto& file : m_ThumbnailToCreate) {
29182918
if (file.use_count()) {
29192919
m_CreateThumbnailFun(&file->thumbnailInfo);
29202920
}
29212921
}
2922-
m_ThumbnailToCreateMutex.lock();
29232922
m_ThumbnailToCreate.clear();
2924-
m_ThumbnailToCreateMutex.unlock();
29252923
}
2924+
m_ThumbnailToCreateMutex.unlock();
29262925
} else {
29272926
printf(
29282927
"No Callback found for create texture\nYou need to define the callback with a call to "
29292928
"SetCreateThumbnailCallback\n");
29302929
}
29312930

29322931
if (m_DestroyThumbnailFun) {
2932+
m_ThumbnailToDestroyMutex.lock();
29332933
if (!m_ThumbnailToDestroy.empty()) {
29342934
for (auto thumbnail : m_ThumbnailToDestroy) {
29352935
m_DestroyThumbnailFun(&thumbnail);
29362936
}
2937-
m_ThumbnailToDestroyMutex.lock();
29382937
m_ThumbnailToDestroy.clear();
2939-
m_ThumbnailToDestroyMutex.unlock();
29402938
}
2939+
m_ThumbnailToDestroyMutex.unlock();
29412940
} else {
29422941
printf(
29432942
"No Callback found for destroy texture\nYou need to define the callback with a call to "

ImGuiFileDialog.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,6 +1362,7 @@ struct IGFD_Thumbnail_Info {
13621362
#include <regex>
13631363
#include <array>
13641364
#include <mutex>
1365+
#include <condition_variable>
13651366
#include <thread>
13661367
#include <cfloat>
13671368
#include <memory>
@@ -1980,6 +1981,7 @@ class IGFD_API ThumbnailFeature {
19801981
std::shared_ptr<std::thread> m_ThumbnailGenerationThread = nullptr;
19811982
std::list<std::shared_ptr<FileInfos>> m_ThumbnailFileDatasToGet; // base container
19821983
std::mutex m_ThumbnailFileDatasToGetMutex;
1984+
std::condition_variable m_ThumbnailFileDatasToGetCv;
19831985
std::list<std::shared_ptr<FileInfos>> m_ThumbnailToCreate; // base container
19841986
std::mutex m_ThumbnailToCreateMutex;
19851987
std::list<IGFD_Thumbnail_Info> m_ThumbnailToDestroy; // base container

0 commit comments

Comments
 (0)