源自于Objective-C的Ref对象,需要使用AutoreleasePool进行内存管理,AutoreleasePool是非线程安全的,所有不推荐在子多线程中调用Ref对象的retain()、 release()和autorelease()等函数。另外,OpenGL上下文对象也是不支持线程安全的。php
但是有的时候咱们需要异步载入一些资源,好比:载入图片纹理、声音的预处理和网络请求数据等。html
假设是异步载入图片纹理咱们可以使用第20.4.4一节介绍的内容。但声音的预处理和网络请求数据等就需要本身经过多线程技术实现了。
Cocos2d-x引擎也提供了多线程技术。Cocos2d-x 3.x以前是使用第三方的pthread技术。ios
Cocos2d-x 3.x以后使用C++11新规范中的std::thread多线程技术,std::thread使用起来比較简单。
1.std::thread多线程技术
std::thread是C++11 引入了一个新的线程库。它提供了线程管理相关函数。std::thread库中还提供了std::mutex(相互排斥量)。经过std::mutex可以实现线程同步。
启动一个新的线程很是easy。当咱们建立一个 std::thread 对象时候,它便会自行启动。建立线程std::thread 对象时。可以提供该线程的回调函数。如下代码实现了建立线程和线程函数的回调:
安全
#include <thread> #include <iostream> void callfn(){ ① std::cout << "Hello thread! " << std::endl; } int main(){ std::thread t1(callfn); ② t1.join(); ③ return 0; }
代码第①行是回调函数的定义。第③行代码t1.join()是将子线程与主线程合并,这样的合并可以使子线程运行完毕后才干继续运行主线程,这是为了不子线程还在运行,主线程已经运行结束而撤销。微信
建立线程还可以使用堆的方式分配内存,代码例如如下:
网络
void callfn(){ std::cout << "Hello thread! " << std::endl; } int main(){ std::thread* t1 = new std::thread(callfn); ① t1->join(); delete t1; ② t1 = nullptr; ③ return 0; }
如下咱们介绍一下异步预处理声音。多线程
咱们在前面20.5一节介绍了声音预处理和清除,在那一节中预处理声音是同步的。它会致使阻塞主线程,使用户的感受会“卡”了一下。假设这个“卡”比較长。咱们解决主线程阻塞问题。改善用户体验,咱们可以异步预处理声音。
咱们在20.5一节的案例中採用std::thread线程异步预处理声音,咱们可以在AppDelegate中进行异步载入,改动以后的AppDelegate.h代码例如如下:
#include "cocos2d.h"
#include "SimpleAudioEngine.h"
using namespace CocosDenshion;
class AppDelegate : private cocos2d::Application
{
private:
std::thread *_loadingAudioThread; ①
void loadingAudio(); ②
public:
AppDelegate();
virtual ~AppDelegate();
… …
};
咱们在第①行声明了私有的std::thread线程指针变量_loadingAudioThread。第②代码是声明了私有的异步预处理声音函数loadingAudio()。
改动以后的AppDelegate.cpp代码例如如下:
并发
include "AppDelegate.h" #include "HelloWorldScene.h" USING_NS_CC; AppDelegate::AppDelegate() { _loadingAudioThread = new std::thread(&AppDelegate::loadingAudio,this); ① } AppDelegate::~AppDelegate() { _loadingAudioThread->join(); ② CC_SAFE_DELETE(_loadingAudioThread); ③ } bool AppDelegate::applicationDidFinishLaunching() { … … return true; } void AppDelegate::applicationDidEnterBackground() { Director::getInstance()->stopAnimation(); SimpleAudioEngine::getInstance()->pauseBackgroundMusic(); } void AppDelegate::applicationWillEnterForeground() { Director::getInstance()->startAnimation(); SimpleAudioEngine::getInstance()->resumeBackgroundMusic(); } void AppDelegate::loadingAudio() ④ { //初始化 音乐 SimpleAudioEngine::getInstance()->preloadBackgroundMusic("sound/Jazz.mp3"); SimpleAudioEngine::getInstance()->preloadBackgroundMusic("sound/Synth.mp3"); //初始化 音效 SimpleAudioEngine::getInstance()->preloadEffect("sound/Blip.wav"); }
第④行代码AppDelegate::loadingAudio() 定义了线程回调函数。咱们在这个函数中预处理声音。app
《Cocos2d-x实战 C++卷》现已上线。各大商店均已开售:异步
京东:http://item.jd.com/11584534.html
亚马逊:http://www.amazon.cn/Cocos2d-x%E5%AE%9E%E6%88%98-C-%E5%8D%B7-%E5%85%B3%E4%B8%9C%E5%8D%87/dp/B00PTYWTLU
当当:http://product.dangdang.com/23606265.html
互动出版网:http://product.china-pub.com/3770734
《Cocos2d-x实战 C++卷》源代码及样章下载地址:
源代码下载地址:http://51work6.com/forum.php?mod=viewthread&tid=1155&extra=page%3D1
样章下载地址:http://51work6.com/forum.php?
mod=viewthread&tid=1157&extra=page%3D1