diff --git a/src/mbgl/tile/tile_cache.cpp b/src/mbgl/tile/tile_cache.cpp index 8dc0bac35..2bbde03a8 100644 --- a/src/mbgl/tile/tile_cache.cpp +++ b/src/mbgl/tile/tile_cache.cpp @@ -53,6 +53,8 @@ void TileCache::deferredRelease(std::unique_ptr&& tile) { // last one and the destruction actually occurs here on this thread. std::function func{[tile_{CaptureWrapper{std::move(tile)}}, this]() mutable { tile_.item = {}; + + std::lock_guard counterLock(deferredSignalLock); deferredDeletionsPending--; deferredSignal.notify_all(); }}; diff --git a/src/mbgl/tile/tile_cache.hpp b/src/mbgl/tile/tile_cache.hpp index 72d430db3..db9f8658c 100644 --- a/src/mbgl/tile/tile_cache.hpp +++ b/src/mbgl/tile/tile_cache.hpp @@ -50,10 +50,9 @@ private: std::map> tiles; std::list orderedKeys; TaggedScheduler threadPool; - std::atomic deferredDeletionsPending{0}; + size_t deferredDeletionsPending{0}; std::mutex deferredSignalLock; std::condition_variable deferredSignal; - size_t size; }; diff --git a/src/mbgl/util/thread_pool.cpp b/src/mbgl/util/thread_pool.cpp index af3f2e6a3..a2e4cc09d 100644 --- a/src/mbgl/util/thread_pool.cpp +++ b/src/mbgl/util/thread_pool.cpp @@ -130,6 +130,8 @@ void ThreadedSchedulerBase::schedule(const util::SimpleIdentity tag, std::functi taskCount++; } + // Take the worker lock before notifying to prevent threads from waiting while we try to wake them + std::lock_guard workerLock(workerMutex); cvAvailable.notify_one(); }