标签:cocos2d-x cocos2d 开发人员 namespace visual studio 2012
多线程并发访问在Cocos2d-x引擎中用的不是很多,这主要是因为中整个结构设计没有采用多线程。源自于Objective-C的Ref对象,需要使用AutoreleasePool进行内存管理,AutoreleasePool是非线程安全的,所有不推荐在子多线程中调用Ref对象的retain()、 release()和autorelease()等函数。另外,OpenGL上下文对象也是不支持线程安全的。#include <thread>
#include <iostream>
void callfn(){ ①
std::cout << "Hello thread! " << std::endl;
}
int main(){
std::thread t1(callfn); ②
t1.join(); ③
return 0;
}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;
}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() 定义了线程回调函数,我们在这个函数中预处理声音。
标签:cocos2d-x cocos2d 开发人员 namespace visual studio 2012
原文地址:http://blog.csdn.net/tonny_guan/article/details/41017763