由于FileUtils线程不安全,异步加载使用FileUtils时使用了互斥量:
CCDataReaderHelper.cpp
......
_dataReaderHelper->_getFileMutex.lock();
unsigned char *pBytes = FileUtils::getInstance()->getFileData(fullPath.c_str() , filereadmode.c_str(), &size);
_dataReaderHelper->_getFileMutex.unlock();
......
_getFileMutex.lock();
ArmatureDataManager::getInstance()->addSpriteFrameFromFile(pAsyncStruct->plistPath.c_str(), pAsyncStruct->imagePath.c_str(), pDataInfo->filename.c_str());
_getFileMutex.unlock();
......
但如果游戏中有其它地方(另一个线程)使用了FileUtils而没有使用相同的互斥量,就可能因异步加载而崩溃。
CCImage.cpp
......
bool Image::initWithImageFileThreadSafe(const std::string& fullpath)
{
bool ret = false;
_filePath = fullpath;
Data data = FileUtils::getInstance()->getDataFromFile(fullpath);
if (!data.isNull())
{
ret = initWithImageData(data.getBytes(), data.getSize());
}
return ret;
}
......
Image::initWithImageFileThreadSafe()并不像它名字所说的线程安全。