0

0

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

php中文网

php中文网

发布时间:2016-06-07 15:09:36

|

1442人浏览过

|

来源于php中文网

原创

//-------------------------------------------------------------------- // // CCPoolManager // //-------------------------------------------------------------------- /////【diff - begin】- by layne////// CCPoolManager* CCPoolManager::shared

//--------------------------------------------------------------------

//

// 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 = 1


    pthread_mutex_lock(&m_mutex);


    CCArray* pCurReleasePoolStack = getCurReleasePoolStack();

    CCAutoreleasePool* pPool = new CCAutoreleasePool();         //ref = 1

    pCurReleasePoolStack->addObject(pPool);                               //ref = 2

    pPool->release();                                           //ref = 1   


    pthread_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 = 1

        pPoolStack->addObject(pPool);                               //ref = 2

        pPool->release();                                           //ref = 1


        pReleasePool = pPool;

    }


    return pReleasePool;

}


/////【diff - end】- by layne//////



代码下载地址:https://github.com/kaitiren/pthread-test-for-cocos2dx

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
php多线程怎么实现
php多线程怎么实现

PHP本身不支持原生多线程,但可通过扩展如pthreads、Swoole或结合多进程、协程等方式实现并发处理。阅读专题下面的文章了解更多详细内容。

0

2026.01.31

php如何运行环境
php如何运行环境

本合集详细介绍PHP运行环境的搭建与配置方法,涵盖Windows、Linux及Mac系统下的安装步骤、常见问题及解决方案。阅读专题下面的文章了解更多详细内容。

0

2026.01.31

php环境变量如何设置
php环境变量如何设置

本合集详细讲解PHP环境变量的设置方法,涵盖Windows、Linux及常见服务器环境配置技巧,助你快速掌握环境变量的正确配置。阅读专题下面的文章了解更多详细内容。

0

2026.01.31

php图片如何上传
php图片如何上传

本合集涵盖PHP图片上传的核心方法、安全处理及常见问题解决方案,适合初学者与进阶开发者。阅读专题下面的文章了解更多详细内容。

2

2026.01.31

Python 数据清洗与预处理实战
Python 数据清洗与预处理实战

本专题系统讲解 Python 在数据清洗与预处理中的核心技术,包括使用 Pandas 进行缺失值处理、异常值检测、数据格式化、特征工程与数据转换,结合 NumPy 高效处理大规模数据。通过实战案例,帮助学习者掌握 如何处理混乱、不完整数据,为后续数据分析与机器学习模型训练打下坚实基础。

1

2026.01.31

C++ 设计模式与软件架构
C++ 设计模式与软件架构

本专题深入讲解 C++ 中的常见设计模式与架构优化,包括单例模式、工厂模式、观察者模式、策略模式、命令模式等,结合实际案例展示如何在 C++ 项目中应用这些模式提升代码可维护性与扩展性。通过案例分析,帮助开发者掌握 如何运用设计模式构建高质量的软件架构,提升系统的灵活性与可扩展性。

37

2026.01.30

c++ 字符串格式化
c++ 字符串格式化

本专题整合了c++字符串格式化用法、输出技巧、实践等等内容,阅读专题下面的文章了解更多详细内容。

18

2026.01.30

java 字符串格式化
java 字符串格式化

本专题整合了java如何进行字符串格式化相关教程、使用解析、方法详解等等内容。阅读专题下面的文章了解更多详细教程。

20

2026.01.30

python 字符串格式化
python 字符串格式化

本专题整合了python字符串格式化教程、实践、方法、进阶等等相关内容,阅读专题下面的文章了解更多详细操作。

6

2026.01.30

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
最新Python教程 从入门到精通
最新Python教程 从入门到精通

共4课时 | 22.4万人学习

Node.js 教程
Node.js 教程

共57课时 | 9.8万人学习

CSS3 教程
CSS3 教程

共18课时 | 5万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号