From f5ed772c220f403593ed8b7a865fffaceb2b3844 Mon Sep 17 00:00:00 2001 From: minggo Date: Mon, 24 Jun 2013 14:11:30 +0800 Subject: [PATCH 1/3] issue #2305:remove unneeded comment --- extensions/AssetsManager/AssetsManager.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/extensions/AssetsManager/AssetsManager.h b/extensions/AssetsManager/AssetsManager.h index c9def9188f..6523e73473 100644 --- a/extensions/AssetsManager/AssetsManager.h +++ b/extensions/AssetsManager/AssetsManager.h @@ -27,7 +27,6 @@ #include #include -//#include #include #include "cocos2d.h" @@ -171,7 +170,6 @@ private: void handleUpdateSucceed(Message *msg); std::list *_messageQueue; - //pthread_mutex_t _messageQueueMutex; std::mutex _messageQueueMutex; }; From 9d44b52ebfa272b438b24e000894519f2cdd447a Mon Sep 17 00:00:00 2001 From: minggo Date: Wed, 26 Jun 2013 14:48:19 +0800 Subject: [PATCH 2/3] issue #2305:use c++11 thread correctly --- cocos2dx/textures/CCTextureCache.cpp | 16 +++++----------- cocos2dx/textures/CCTextureCache.h | 3 +++ 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/cocos2dx/textures/CCTextureCache.cpp b/cocos2dx/textures/CCTextureCache.cpp index 42b19843cf..9eb222d817 100644 --- a/cocos2dx/textures/CCTextureCache.cpp +++ b/cocos2dx/textures/CCTextureCache.cpp @@ -28,7 +28,6 @@ THE SOFTWARE. #include #include #include -#include #include "CCTextureCache.h" #include "CCTexture2D.h" @@ -70,7 +69,8 @@ TextureCache * TextureCache::sharedTextureCache() } TextureCache::TextureCache() -: _asyncStructQueue(nullptr) +: _loadingThread(nullptr) +, _asyncStructQueue(nullptr) , _imageInfoQueue(nullptr) , _needQuit(false) , _asyncRefCount(0) @@ -85,6 +85,7 @@ TextureCache::~TextureCache() CC_SAFE_RELEASE(_textures); + CC_SAFE_DELETE(_loadingThread); _sharedTextureCache = nullptr; } @@ -92,8 +93,8 @@ void TextureCache::purgeSharedTextureCache() { // notify sub thread to quick _sharedTextureCache->_needQuit = true; - std::lock_guard lk(_sharedTextureCache->_sleepMutex); _sharedTextureCache->_sleepCondition.notify_one(); + _sharedTextureCache->_loadingThread->join(); CC_SAFE_RELEASE_NULL(_sharedTextureCache); } @@ -146,10 +147,7 @@ void TextureCache::addImageAsync(const char *path, Object *target, SEL_CallFuncO _imageInfoQueue = new queue(); // create a new thread to load images - auto t = std::thread(&TextureCache::loadImage, this); - t.detach(); - // should retain here, because sub thread use invoke TextureCache::loadImage() - retain(); + _loadingThread = new std::thread(&TextureCache::loadImage, this); _needQuit = false; } @@ -174,7 +172,6 @@ void TextureCache::addImageAsync(const char *path, Object *target, SEL_CallFuncO _asyncStructQueue->push(data); _asyncStructQueueMutex.unlock(); - std::lock_guard lk(_sleepMutex); _sleepCondition.notify_one(); } @@ -249,9 +246,6 @@ void TextureCache::loadImage() delete _imageInfoQueue; _imageInfoQueue = nullptr; } - - // should release here, because we retain it when creating a sub thread in addImageAsync() - release(); } Image::EImageFormat TextureCache::computeImageFormatType(string& filename) diff --git a/cocos2dx/textures/CCTextureCache.h b/cocos2dx/textures/CCTextureCache.h index 5227ef275f..307ca8c442 100644 --- a/cocos2dx/textures/CCTextureCache.h +++ b/cocos2dx/textures/CCTextureCache.h @@ -29,6 +29,7 @@ THE SOFTWARE. #include #include +#include #include #include #include @@ -174,6 +175,8 @@ protected: Image *image; Image::EImageFormat imageType; } ImageInfo; + + std::thread* _loadingThread; std::queue* _asyncStructQueue; std::queue* _imageInfoQueue; From ca2966745ecda576ab50bb40e5d8da0e8ebe20db Mon Sep 17 00:00:00 2001 From: minggo Date: Wed, 26 Jun 2013 15:11:13 +0800 Subject: [PATCH 3/3] closed #2305: fix a running time error --- cocos2dx/textures/CCTextureCache.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos2dx/textures/CCTextureCache.cpp b/cocos2dx/textures/CCTextureCache.cpp index 9eb222d817..8176f7d3c3 100644 --- a/cocos2dx/textures/CCTextureCache.cpp +++ b/cocos2dx/textures/CCTextureCache.cpp @@ -94,7 +94,7 @@ void TextureCache::purgeSharedTextureCache() // notify sub thread to quick _sharedTextureCache->_needQuit = true; _sharedTextureCache->_sleepCondition.notify_one(); - _sharedTextureCache->_loadingThread->join(); + if (_sharedTextureCache->_loadingThread) _sharedTextureCache->_loadingThread->join(); CC_SAFE_RELEASE_NULL(_sharedTextureCache); }