Skip to content

Commit

Permalink
Fixed an occasional crash resulting from multi-thread access of the i…
Browse files Browse the repository at this point in the history
…mage cache

This was a problem as of 52db950 since the loading
screen could access the image cache while the worker thread cleared it.
  • Loading branch information
Vultraz committed May 29, 2018
1 parent 6d8957c commit 0eb27e2
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/image.cpp
Expand Up @@ -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");
Expand Down Expand Up @@ -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>
Expand Down

0 comments on commit 0eb27e2

Please sign in to comment.