Merge pull request #3006 from minggo/iss2305-replace-pthread

fix some errors of c++11 thread usage in CCTextureCache
This commit is contained in:
minggo 2013-06-26 00:40:17 -07:00
commit d3c10408ff
3 changed files with 8 additions and 12 deletions

View File

@ -28,7 +28,6 @@ THE SOFTWARE.
#include <stack> #include <stack>
#include <cctype> #include <cctype>
#include <list> #include <list>
#include <thread>
#include "CCTextureCache.h" #include "CCTextureCache.h"
#include "CCTexture2D.h" #include "CCTexture2D.h"
@ -70,7 +69,8 @@ TextureCache * TextureCache::sharedTextureCache()
} }
TextureCache::TextureCache() TextureCache::TextureCache()
: _asyncStructQueue(nullptr) : _loadingThread(nullptr)
, _asyncStructQueue(nullptr)
, _imageInfoQueue(nullptr) , _imageInfoQueue(nullptr)
, _needQuit(false) , _needQuit(false)
, _asyncRefCount(0) , _asyncRefCount(0)
@ -85,6 +85,7 @@ TextureCache::~TextureCache()
CC_SAFE_RELEASE(_textures); CC_SAFE_RELEASE(_textures);
CC_SAFE_DELETE(_loadingThread);
_sharedTextureCache = nullptr; _sharedTextureCache = nullptr;
} }
@ -92,8 +93,8 @@ void TextureCache::purgeSharedTextureCache()
{ {
// notify sub thread to quick // notify sub thread to quick
_sharedTextureCache->_needQuit = true; _sharedTextureCache->_needQuit = true;
std::lock_guard<std::mutex> lk(_sharedTextureCache->_sleepMutex);
_sharedTextureCache->_sleepCondition.notify_one(); _sharedTextureCache->_sleepCondition.notify_one();
if (_sharedTextureCache->_loadingThread) _sharedTextureCache->_loadingThread->join();
CC_SAFE_RELEASE_NULL(_sharedTextureCache); CC_SAFE_RELEASE_NULL(_sharedTextureCache);
} }
@ -146,10 +147,7 @@ void TextureCache::addImageAsync(const char *path, Object *target, SEL_CallFuncO
_imageInfoQueue = new queue<ImageInfo*>(); _imageInfoQueue = new queue<ImageInfo*>();
// create a new thread to load images // create a new thread to load images
auto t = std::thread(&TextureCache::loadImage, this); _loadingThread = new std::thread(&TextureCache::loadImage, this);
t.detach();
// should retain here, because sub thread use invoke TextureCache::loadImage()
retain();
_needQuit = false; _needQuit = false;
} }
@ -174,7 +172,6 @@ void TextureCache::addImageAsync(const char *path, Object *target, SEL_CallFuncO
_asyncStructQueue->push(data); _asyncStructQueue->push(data);
_asyncStructQueueMutex.unlock(); _asyncStructQueueMutex.unlock();
std::lock_guard<std::mutex> lk(_sleepMutex);
_sleepCondition.notify_one(); _sleepCondition.notify_one();
} }
@ -249,9 +246,6 @@ void TextureCache::loadImage()
delete _imageInfoQueue; delete _imageInfoQueue;
_imageInfoQueue = nullptr; _imageInfoQueue = nullptr;
} }
// should release here, because we retain it when creating a sub thread in addImageAsync()
release();
} }
Image::EImageFormat TextureCache::computeImageFormatType(string& filename) Image::EImageFormat TextureCache::computeImageFormatType(string& filename)

View File

@ -29,6 +29,7 @@ THE SOFTWARE.
#include <string> #include <string>
#include <mutex> #include <mutex>
#include <thread>
#include <condition_variable> #include <condition_variable>
#include <queue> #include <queue>
#include <string> #include <string>
@ -175,6 +176,8 @@ protected:
Image::EImageFormat imageType; Image::EImageFormat imageType;
} ImageInfo; } ImageInfo;
std::thread* _loadingThread;
std::queue<AsyncStruct*>* _asyncStructQueue; std::queue<AsyncStruct*>* _asyncStructQueue;
std::queue<ImageInfo*>* _imageInfoQueue; std::queue<ImageInfo*>* _imageInfoQueue;

View File

@ -27,7 +27,6 @@
#include <string> #include <string>
#include <curl/curl.h> #include <curl/curl.h>
//#include <pthread.h>
#include <mutex> #include <mutex>
#include "cocos2d.h" #include "cocos2d.h"