您的位置:首页 > 移动开发 > Cocos引擎

cocos2dx多线程以及线程同步 与 cocos2dx内存管理与多线程问题

2016-04-21 14:49 260 查看
cocos2d-x引擎在内部实现了一个庞大的主循环,每帧之间更新界面,如果耗时的操作放到了主线程中,游戏的界面就会卡,这是不能容忍的,游戏最基本的条件就是流畅性,这就是为什么游戏开发选择C++的原因。另外现在双核手机和四核手机越来越普遍了,是时候使用多线程来挖掘硬件的潜力了。

1.环境搭建

cocos2d-x中的多线程使用pthread就可以实现跨平台,而且也不是很难理解。使用pthread需要先配置一下工程。右击工程----->属性----->配置属性---->链接器----->输入---->附加依赖项中添加pthreadVCE2.lib,如下图接着添加附加包含目录,右击项目,属性----->C/C++---->常规----->附加包含目录加入pthread头文件所在的目录这样,环境就搭建起来了。

2.多线程的使用

使用pthread来实现多线程,最重要的一个函数是[cpp] view
plaincopyPTW32_DLLPORT int PTW32_CDECL pthread_create (pthread_t * tid,//线程的标示const pthread_attr_t * attr, //创建线程的参数void *(*start) (void *), //入口函数的指针void *arg); //传递给线程的数据[cpp] view
plaincopyPTW32_DLLPORT int PTW32_CDECL pthread_create (pthread_t * tid,//线程的标示const pthread_attr_t * attr, //创建线程的参数void *(*start) (void *), //入口函数的指针void *arg); //传递给线程的数据在HelloWorldScene.h文件中[cpp] view
plaincopypthread_t pidrun,pidgo;static void* th_run(void *r);static void* th_go(void *r);[cpp] view
plaincopypthread_t pidrun,pidgo;static void* th_run(void *r);static void* th_go(void *r);定义了两个函数和两个线程的标识。然后自定义了一个类,用于给线程传递数据。Student类如下:[cpp] view
plaincopy#pragma once#include <string>class Student{public:Student(void);Student(std::string name,int age,std::string sex);~Student(void);std::string name;int age;std::string sex;};[cpp] view
plaincopy#pragma once#include <string>class Student{public:Student(void);Student(std::string name,int age,std::string sex);~Student(void);std::string name;int age;std::string sex;};源文件如下[cpp] view
plaincopy#include "Student.h"#include "cocos2d.h"Student::Student(void){}Student::~Student(void){cocos2d::CCLog("delete data");}Student::Student(std::string name,int age,std::string sex){this->name=name;this->age=age;this->sex=sex;}[cpp] view
plaincopy#include "Student.h"#include "cocos2d.h"Student::Student(void){}Student::~Student(void){cocos2d::CCLog("delete data");}Student::Student(std::string name,int age,std::string sex){this->name=name;this->age=age;this->sex=sex;}在退出菜单的回调函数中启动两个线程:[cpp] view
plaincopyvoid HelloWorld::menuCloseCallback(CCObject* pSender){Student *temp=new Student(std::string("zhycheng"),23,std::string("male"));pthread_mutex_init(&mutex,NULL);pthread_create(&pidrun,NULL,th_run,temp);//启动线程pthread_create(&pidgo,NULL,th_go,0);}[cpp] view
plaincopyvoid HelloWorld::menuCloseCallback(CCObject* pSender){Student *temp=new Student(std::string("zhycheng"),23,std::string("male"));pthread_mutex_init(&mutex,NULL);pthread_create(&pidrun,NULL,th_run,temp);//启动线程pthread_create(&pidgo,NULL,th_go,0);}可以看到,将Student的指针传递给了pidrun线程,那么在pidrun线程中获得Student信息如下:[cpp] view
plaincopyStudent *s=(Student*)(r);CCLog("name is %s,and age is %d,sex is %s",s->name.c_str(),s->age,s->sex.c_str());delete s;[cpp] view
plaincopyStudent *s=(Student*)(r);CCLog("name is %s,and age is %d,sex is %s",s->name.c_str(),s->age,s->sex.c_str());delete s;

3.线程同步

使用了线程,必然就要考虑到线程同步,不同的线程同时访问资源的话,访问的顺序是不可预知的,会造成不可预知的结果。这里使用pthread_mutex_t来实现同步,下面我来演示一下使用多线程实现卖票系统。卖票的时候,是由多个窗口同时卖票,这里要做到一张票不要卖出去两次,不要出现有票却无法卖的结果。在线程函数th_run和th_go中来卖票,票的数量是一个全局变量,每卖出去一张票,就将票的数量减一。其中同步的pthread_mutex_t也是一个全局变量,就用它来实现线程同步。[cpp] view
plaincopyvoid* HelloWorld::th_run(void *r){Student *s=(Student*)(r);CCLog("name is %s,and age is %d,sex is %s",s->name.c_str(),s->age,s->sex.c_str());delete s;while(true){pthread_mutex_lock(&mutex);if(ticket>0){CCLog("thread run sell %d",ticket);ticket--;pthread_mutex_unlock(&mutex);}else{pthread_mutex_unlock(&mutex);break;}Sleep(1);//Usleep(10);}return NULL;}[cpp] view
plaincopyvoid* HelloWorld::th_run(void *r){Student *s=(Student*)(r);CCLog("name is %s,and age is %d,sex is %s",s->name.c_str(),s->age,s->sex.c_str());delete s;while(true){pthread_mutex_lock(&mutex);if(ticket>0){CCLog("thread run sell %d",ticket);ticket--;pthread_mutex_unlock(&mutex);}else{pthread_mutex_unlock(&mutex);break;}Sleep(1);//Usleep(10);}return NULL;}[cpp] view
plaincopyvoid* HelloWorld::th_go(void *r){while(true){pthread_mutex_lock(&mutex);if(ticket>0){CCLog("thread go sell %d",ticket);ticket--;pthread_mutex_unlock(&mutex);}else{pthread_mutex_unlock(&mutex);break;}Sleep(1);}return NULL;}[cpp] view
plaincopyvoid* HelloWorld::th_go(void *r){while(true){pthread_mutex_lock(&mutex);if(ticket>0){CCLog("thread go sell %d",ticket);ticket--;pthread_mutex_unlock(&mutex);}else{pthread_mutex_unlock(&mutex);break;}Sleep(1);}return NULL;}mutex被锁定后,其他线程若再想锁定mutex的话,必须等待,当该线程释放了mutex之后,其他线程才能锁定mutex。Sleep()函数可以使得该线程休眠,单位是毫秒。下面是卖票的结果:[cpp] view
plaincopyname is zhycheng,and age is 23,sex is maledelete datathread run sell 100thread run sell 99thread go sell 98thread go sell 97thread run sell 96thread go sell 95thread go sell 94thread run sell 93thread go sell 92thread run sell 91thread go sell 90thread go sell 89thread run sell 88thread go sell 87thread run sell 86thread go sell 85thread run sell 84thread go sell 83thread run sell 82thread go sell 81thread run sell 80thread go sell 79thread run sell 78thread go sell 77thread run sell 76thread go sell 75thread run sell 74thread go sell 73thread run sell 72thread go sell 71thread run sell 70thread go sell 69thread go sell 68thread run sell 67thread go sell 66thread run sell 65thread go sell 64thread run sell 63thread go sell 62thread run sell 61thread go sell 60thread run sell 59thread go sell 58thread run sell 57thread go sell 56thread run sell 55thread go sell 54thread run sell 53thread run sell 52thread go sell 51thread run sell 50thread go sell 49thread run sell 48thread go sell 47thread run sell 46thread go sell 45thread run sell 44thread run sell 43thread go sell 42thread run sell 41thread run sell 40thread go sell 39thread run sell 38thread run sell 37thread run sell 36thread run sell 35thread go sell 34thread run sell 33thread run sell 32thread go sell 31thread run sell 30thread run sell 29thread run sell 28thread run sell 27thread run sell 26thread run sell 25thread go sell 24thread run sell 23thread go sell 22thread go sell 21thread run sell 20thread go sell 19thread run sell 18thread run sell 17thread go sell 16thread run sell 15thread go sell 14thread go sell 13thread run sell 12thread go sell 11thread go sell 10thread run sell 9thread go sell 8thread run sell 7thread go sell 6thread go sell 5thread run sell 4thread go sell 3thread run sell 2thread run sell 1[cpp] view
plaincopyname is zhycheng,and age is 23,sex is maledelete datathread run sell 100thread run sell 99thread go sell 98thread go sell 97thread run sell 96thread go sell 95thread go sell 94thread run sell 93thread go sell 92thread run sell 91thread go sell 90thread go sell 89thread run sell 88thread go sell 87thread run sell 86thread go sell 85thread run sell 84thread go sell 83thread run sell 82thread go sell 81thread run sell 80thread go sell 79thread run sell 78thread go sell 77thread run sell 76thread go sell 75thread run sell 74thread go sell 73thread run sell 72thread go sell 71thread run sell 70thread go sell 69thread go sell 68thread run sell 67thread go sell 66thread run sell 65thread go sell 64thread run sell 63thread go sell 62thread run sell 61thread go sell 60thread run sell 59thread go sell 58thread run sell 57thread go sell 56thread run sell 55thread go sell 54thread run sell 53thread run sell 52thread go sell 51thread run sell 50thread go sell 49thread run sell 48thread go sell 47thread run sell 46thread go sell 45thread run sell 44thread run sell 43thread go sell 42thread run sell 41thread run sell 40thread go sell 39thread run sell 38thread run sell 37thread run sell 36thread run sell 35thread go sell 34thread run sell 33thread run sell 32thread go sell 31thread run sell 30thread run sell 29thread run sell 28thread run sell 27thread run sell 26thread run sell 25thread go sell 24thread run sell 23thread go sell 22thread go sell 21thread run sell 20thread go sell 19thread run sell 18thread run sell 17thread go sell 16thread run sell 15thread go sell 14thread go sell 13thread run sell 12thread go sell 11thread go sell 10thread run sell 9thread go sell 8thread run sell 7thread go sell 6thread go sell 5thread run sell 4thread go sell 3thread run sell 2thread run sell 1可以看到,这个打印结果正确无误。如果不加mutex会是什么样的结果呢,我将线程同步的mutex注释掉,输出的结果为:[cpp] view
plaincopyname is zhycheng,and age is 23,sex is maledelete datathread run sell 100thread run sell 99thread run sell 98thread go sell 97thread run sell 96thread go sell 95thread run sell 94thread go sell 94thread run sell 92thread run sell 91thread go sell 90thread run sell 89thread go sell 88thread run sell 87thread run sell 86thread go sell 86thread go sell 84thread run sell 83thread go sell 82thread run sell 81thread go sell 80thread run sell 79thread run sell 78thread go sell 77thread run sell 76thread run sell 75thread go sell 74thread run sell 73thread go sell 72thread run sell 71thread go sell 70thread go sell 69thread run sell 68thread go sell 67thread go sell 66thread run sell 65thread go sell 64thread go sell 63thread run sell 62thread go sell 61thread run sell 60thread run sell 59thread run sell 58thread run sell 57thread run sell 56thread run sell 55thread go sell 54thread run sell 54thread go sell 52thread run sell 52thread go sell 50thread run sell 50thread go sell 49thread run sell 47thread go sell 47thread go sell 45thread run sell 45thread run sell 43thread go sell 43thread run sell 41thread go sell 41thread go sell 39thread run sell 39thread run sell 37thread go sell 37thread go sell 35thread run sell 35thread go sell 33thread run sell 33thread go sell 31thread run sell 31thread go sell 29thread run sell 29thread go sell 27thread run sell 27thread go sell 25thread run sell 25thread run sell 23thread go sell 23thread run sell 21thread go sell 21thread go sell 19thread run sell 19thread run sell 17thread go sell 17thread go sell 15thread run sell 15thread run sell 13thread go sell 13thread run sell 11thread go sell 11thread go sell 9thread run sell 9thread run sell 7thread go sell 7thread go sell 5thread run sell 5thread go sell 3thread run sell 3thread go sell 1thread run sell 1[cpp] view
plaincopyname is zhycheng,and age is 23,sex is maledelete datathread run sell 100thread run sell 99thread run sell 98thread go sell 97thread run sell 96thread go sell 95thread run sell 94thread go sell 94thread run sell 92thread run sell 91thread go sell 90thread run sell 89thread go sell 88thread run sell 87thread run sell 86thread go sell 86thread go sell 84thread run sell 83thread go sell 82thread run sell 81thread go sell 80thread run sell 79thread run sell 78thread go sell 77thread run sell 76thread run sell 75thread go sell 74thread run sell 73thread go sell 72thread run sell 71thread go sell 70thread go sell 69thread run sell 68thread go sell 67thread go sell 66thread run sell 65thread go sell 64thread go sell 63thread run sell 62thread go sell 61thread run sell 60thread run sell 59thread run sell 58thread run sell 57thread run sell 56thread run sell 55thread go sell 54thread run sell 54thread go sell 52thread run sell 52thread go sell 50thread run sell 50thread go sell 49thread run sell 47thread go sell 47thread go sell 45thread run sell 45thread run sell 43thread go sell 43thread run sell 41thread go sell 41thread go sell 39thread run sell 39thread run sell 37thread go sell 37thread go sell 35thread run sell 35thread go sell 33thread run sell 33thread go sell 31thread run sell 31thread go sell 29thread run sell 29thread go sell 27thread run sell 27thread go sell 25thread run sell 25thread run sell 23thread go sell 23thread run sell 21thread go sell 21thread go sell 19thread run sell 19thread run sell 17thread go sell 17thread go sell 15thread run sell 15thread run sell 13thread go sell 13thread run sell 11thread go sell 11thread go sell 9thread run sell 9thread run sell 7thread go sell 7thread go sell 5thread run sell 5thread go sell 3thread run sell 3thread go sell 1thread run sell 1可以看到,有的票卖了两次,有的票就没卖。

4.注意

1.Sleep()函数是使得线程休眠的函数,这个函数不跨平台,仅仅在windows上能用,其他平台使用usleep。2.在非主线程中不能使用cocos2d-x管理内存的
CCObject::retain()
,
CCObject::release()
CCObject::autorelease(),因为CCAutoreleasePool不是线程安全的,OPENGL的上下文也不是线程安全的,所以不要再非主线程中使用cocos2d-x的API和UI操作。
cocos2dx内存管理与多线程问题:
Cocos2d-x的内存管理采用Objective-C的机制,大喜过望。因为只要坚持Objective-C的原则“谁创建谁释放,谁备份谁释放”的原则即可确保内存使用不易出现Bug。但是因为本身开放的游戏需要使用到多线程技术,导致测试的时候总是莫名其妙的导致空指针错误。而且是随机出现,纠结了2天无果后,开始怀疑Cocos2d-X的内存本身管理可能存在问题。怀着这样的想法,一步一步的调试,发现经常出现指针异常的变量总是在调用autorelease后一会就莫名其妙再使用的时候就抛异常。狠下心,在它的析构函数里面断点+Log输出信息。发现对象被释放了。一时也很迷糊,因为对象只是autorelease,并没有真正释放,是谁导致它释放的?然后就去看了CCAutoreleasePool的源码,发现存在Cocos2d-X的内存管理在多线程的情况下存在如下问题如图:thread 1和thread 2是独立的两个线程,它们之间存在CPU分配的交叉集,我们在time 1的时候push一个autorelease的自动释放池,在该线程的末尾,即time 3的时候pop它。同理在thread 2的线程里面,在time 2的时候push一个自动释放池,在time 4的时候释放它,即Pop.此时我们假设在thread 2分配得到CPU的时候有一个对象obj自动释放,即obj-autorelease().那么在time
3的时候会发生是么事情呢?答案很简单,就是obj在time 3的时候就被释放了,而我们期望它在time 4的时候才释放。所以就导致我上面说的,在多线程下面,cocos2d-x的autorelease变量会发生莫名其妙的指针异常。解决办法:在PoolManager给每个线程根据pthread_t的线程id生成一个CCArray的stack的嵌套管理自动释放池。源码如下所以我在Push的时候根据当前线程的pthread_t的线程id生成一个CCArray的stack来存储该线程对应的Autoreleasepool的嵌套对象源码如下

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
//--------------------------------------------------------------------//// CCPoolManager////--------------------------------------------------------------------/////【diff - begin】- by layne//////CCPoolManager* CCPoolManager::sharedPoolManager(){if (s_pPoolManager == NULL){s_pPoolManager = new CCPoolManager();}return s_pPoolManager;}void CCPoolManager::purgePoolManager(){CC_SAFE_DELETE(s_pPoolManager);}CCPoolManager::CCPoolManager(){// m_pReleasePoolStack = new CCArray();// m_pReleasePoolStack->init();// m_pCurReleasePool = 0;m_pReleasePoolMultiStack = new CCDictionary();}CCPoolManager::~CCPoolManager(){// finalize();// // we only release the last autorelease pool here// m_pCurReleasePool = 0;// m_pReleasePoolStack->removeObjectAtIndex(0);//// CC_SAFE_DELETE(m_pReleasePoolStack);finalize();CC_SAFE_DELETE(m_pReleasePoolMultiStack);}void CCPoolManager::finalize(){if(m_pReleasePoolMultiStack->count() > 0){//CCAutoreleasePool* pReleasePool;CCObject* pkey = NULL;CCARRAY_FOREACH(m_pReleasePoolMultiStack->allKeys(), pkey){if(!pkey)break;CCInteger *key = (CCInteger*)pkey;CCArray *poolStack = (CCArray *)m_pReleasePoolMultiStack->objectForKey(key->getValue());CCObject* pObj = NULL;CCARRAY_FOREACH(poolStack, pObj){if(!pObj)break;CCAutoreleasePool* pPool = (CCAutoreleasePool*)pObj;pPool->clear();}}}}void CCPoolManager::push(){// CCAutoreleasePool* pPool = new CCAutoreleasePool(); //ref = 1// m_pCurReleasePool = pPool;//// m_pReleasePoolStack->addObject(pPool); //ref = 2//// pPool->release(); //ref = 1pthread_mutex_lock(&m_mutex);CCArray* pCurReleasePoolStack = getCurReleasePoolStack();CCAutoreleasePool* pPool = new CCAutoreleasePool(); //ref = 1pCurReleasePoolStack->addObject(pPool); //ref = 2pPool->release(); //ref = 1pthread_mutex_unlock(&m_mutex);}void CCPoolManager::pop(){// if (! m_pCurReleasePool)// {// return;// }//// int nCount = m_pReleasePoolStack->count();//// m_pCurReleasePool->clear();//// if(nCount > 1)// {// m_pReleasePoolStack->removeObjectAtIndex(nCount-1);//// // if(nCount > 1)// // {// // m_pCurReleasePool = m_pReleasePoolStack->objectAtIndex(nCount - 2);// // return;// // }// m_pCurReleasePool = (CCAutoreleasePool*)m_pReleasePoolStack->objectAtIndex(nCount - 2);// }//// /*m_pCurReleasePool = NULL;*/pthread_mutex_lock(&m_mutex);CCArray* pCurReleasePoolStack = getCurReleasePoolStack();CCAutoreleasePool* pCurReleasePool = getCurReleasePool();if (pCurReleasePoolStack && pCurReleasePool){int nCount = pCurReleasePoolStack->count();pCurReleasePool->clear();if(nCount > 1){pCurReleasePoolStack->removeObject(pCurReleasePool);}}pthread_mutex_unlock(&m_mutex);}void CCPoolManager::removeObject(CCObject* pObject){// CCAssert(m_pCurReleasePool, "current auto release pool should not be null");//// m_pCurReleasePool->removeObject(pObject);pthread_mutex_lock(&m_mutex);CCAutoreleasePool* pCurReleasePool = getCurReleasePool();CCAssert(pCurReleasePool, "current auto release pool should not be null");pCurReleasePool->removeObject(pObject);pthread_mutex_unlock(&m_mutex);}void CCPoolManager::addObject(CCObject* pObject){// getCurReleasePool()->addObject(pObject);pthread_mutex_lock(&m_mutex);CCAutoreleasePool* pCurReleasePool = getCurReleasePool(true);CCAssert(pCurReleasePool, "current auto release pool should not be null");pCurReleasePool->addObject(pObject);pthread_mutex_unlock(&m_mutex);}CCArray* CCPoolManager::getCurReleasePoolStack(){CCArray* pPoolStack = NULL;pthread_t tid = pthread_self();if(m_pReleasePoolMultiStack->count() > 0){pPoolStack = (CCArray*)m_pReleasePoolMultiStack->objectForKey((int)tid);}if (!pPoolStack) {pPoolStack = new CCArray();m_pReleasePoolMultiStack->setObject(pPoolStack, (int)tid);pPoolStack->release();}return pPoolStack;}CCAutoreleasePool* CCPoolManager::getCurReleasePool(bool autoCreate){// if(!m_pCurReleasePool)// {// push();// }//// CCAssert(m_pCurReleasePool, "current auto release pool should not be null");//// return m_pCurReleasePool;CCAutoreleasePool* pReleasePool = NULL;CCArray* pPoolStack = getCurReleasePoolStack();if(pPoolStack->count() > 0){pReleasePool = (CCAutoreleasePool*)pPoolStack->lastObject();}if (!pReleasePool && autoCreate) {CCAutoreleasePool* pPool = new CCAutoreleasePool(); //ref = 1pPoolStack->addObject(pPool); //ref = 2pPool->release(); //ref = 1pReleasePool = pPool;}return pReleasePool;}/////【diff - end】- by layne//////代码下载地址:https://github.com/kaitiren/pthread-test-for-cocos2dx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: