@@ -214,8 +214,8 @@ void TextureCache::addImageAsync(const std::string &path, const std::function<vo
214
214
if (_loadingThread == nullptr )
215
215
{
216
216
// create a new thread to load images
217
- _loadingThread = new (std::nothrow) std::thread (&TextureCache::loadImage, this );
218
217
_needQuit = false ;
218
+ _loadingThread = new (std::nothrow) std::thread (&TextureCache::loadImage, this );
219
219
}
220
220
221
221
if (0 == _asyncRefCount)
@@ -231,10 +231,8 @@ void TextureCache::addImageAsync(const std::string &path, const std::function<vo
231
231
232
232
// add async struct into queue
233
233
_asyncStructQueue.push_back (data);
234
- _requestMutex. lock ( );
234
+ std::unique_lock<std::mutex> ul (_requestMutex );
235
235
_requestQueue.push_back (data);
236
- _requestMutex.unlock ();
237
-
238
236
_sleepCondition.notify_one ();
239
237
}
240
238
@@ -270,12 +268,10 @@ void TextureCache::unbindAllImageAsync()
270
268
void TextureCache::loadImage ()
271
269
{
272
270
AsyncStruct *asyncStruct = nullptr ;
273
- std::mutex signalMutex;
274
- std::unique_lock<std::mutex> signal (signalMutex);
275
271
while (!_needQuit)
276
272
{
273
+ std::unique_lock<std::mutex> ul (_requestMutex);
277
274
// pop an AsyncStruct from request queue
278
- _requestMutex.lock ();
279
275
if (_requestQueue.empty ())
280
276
{
281
277
asyncStruct = nullptr ;
@@ -285,12 +281,15 @@ void TextureCache::loadImage()
285
281
asyncStruct = _requestQueue.front ();
286
282
_requestQueue.pop_front ();
287
283
}
288
- _requestMutex.unlock ();
289
284
290
285
if (nullptr == asyncStruct) {
291
- _sleepCondition.wait (signal);
286
+ if (_needQuit) {
287
+ break ;
288
+ }
289
+ _sleepCondition.wait (ul);
292
290
continue ;
293
291
}
292
+ ul.unlock ();
294
293
295
294
// load image
296
295
asyncStruct->loadSuccess = asyncStruct->image .initWithImageFileThreadSafe (asyncStruct->filename );
@@ -660,8 +659,10 @@ std::string TextureCache::getTextureFilePath(cocos2d::Texture2D* texture) const
660
659
void TextureCache::waitForQuit ()
661
660
{
662
661
// notify sub thread to quick
662
+ std::unique_lock<std::mutex> ul (_requestMutex);
663
663
_needQuit = true ;
664
664
_sleepCondition.notify_one ();
665
+ ul.unlock ();
665
666
if (_loadingThread) _loadingThread->join ();
666
667
}
667
668
0 commit comments