Fixed an occasional crash resulting from multi-thread access of the image cache

This was a problem as of 52db950e94 since the loading
screen could access the image cache while the worker thread cleared it.

(cherry-picked from commit 6d0b7c8424)
This commit is contained in:
Charles Dang 2018-05-29 13:59:43 +11:00
parent 6b5ce73605
commit 504435023c

View file

@ -41,6 +41,7 @@
#include <boost/algorithm/string.hpp>
#include <boost/functional/hash_fwd.hpp>
#include <mutex>
#include <set>
static lg::log_domain log_display("display");
@ -105,23 +106,30 @@ class cache_type
public:
cache_type()
: content_()
, cache_lock_()
{
}
cache_item<T>& get_element(int index)
{
if(static_cast<unsigned>(index) >= content_.size())
std::lock_guard<std::mutex> lock(cache_lock_);
if(static_cast<unsigned>(index) >= content_.size()) {
content_.resize(index + 1);
}
return content_[index];
}
void flush()
{
std::lock_guard<std::mutex> lock(cache_lock_);
content_.clear();
}
private:
std::vector<cache_item<T>> content_;
std::mutex cache_lock_;
};
template<typename T>