加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 17:10:22 所属栏目:百科 来源:网络整理
导读:ocos2d-x引擎在内部实现了一个庞大的主循环,每帧之间更新界面,如果耗时的操作放到了主线程中,游戏的界面就会卡,这是不能容忍的,游戏最基本的条件就是流畅性,这就是为什么游戏开发选择C++的原因。另外现在双核手机和四核手机越来越普遍了,是时候使用多
ocos2d-x引擎在内部实现了一个庞大的主循环,每帧之间更新界面,如果耗时的操作放到了主线程中,游戏的界面就会卡,这是不能容忍的,游戏最基本的条件就是流畅性,这就是为什么游戏开发选择C++的原因。另外现在双核手机和四核手机越来越普遍了,是时候使用多线程来挖掘硬件的潜力了。

1.环境搭建

cocos2d-x中的多线程使用pthread就可以实现跨平台,而且也不是很难理解。使用pthread需要先配置一下工程。右击工程----->属性----->配置属性---->链接器----->输入---->附加依赖项中添加pthreadVCE2.lib,如下图


接着添加附加包含目录,右击项目,属性----->C/C++---->常规----->附加包含目录加入pthread头文件所在的目录


这样,环境就搭建起来了。

2.多线程的使用

使用pthread来实现多线程,最重要的一个函数是

[cpp] view plain copy
  1. PTW32_DLLPORTintPTW32_CDECLpthread_create(pthread_t*tid,//线程的标示
  2. constpthread_attr_t*attr,//创建线程的参数
  3. void*(*start)(void*),//入口函数的指针
  4. void*arg);//传递给线程的数据
[cpp] view plain copy
    PTW32_DLLPORTintPTW32_CDECLpthread_create(pthread_t*tid,//线程的标示
  1. constpthread_attr_t*attr,//创建线程的参数
  2. void*(*start)(void*),//入口函数的指针
  3. void*arg);//传递给线程的数据

在HelloWorldScene.h文件中

copy
    pthread_tpidrun,pidgo;
  1. staticvoid*th_run(void*r);
  2. staticvoid*th_go(void*r);
copy
    pthread_tpidrun,pidgo;
  1. staticvoid*th_run(void*r);
  2. staticvoid*th_go(void*r);
定义了两个函数和两个线程的标识。

然后自定义了一个类,用于给线程传递数据。Student类如下:

copy
    #pragmaonce
  1. #include<string>
  2. classStudent
  3. {
  4. public:
  5. Student(void);
  6. Student(std::stringname,intage,std::stringsex);
  7. ~Student(void);
  8. std::stringname;
  9. intage;
  10. std::stringsex;
  11. };
copy
    #pragmaonce
  1. #include<string>
  2. classStudent
  3. {
  4. public:
  5. Student(void);
  6. Student(std::stringname,intage,std::stringsex);
  7. ~Student(void);
  8. std::stringname;
  9. intage;
  10. std::stringsex;
  11. };

源文件如下

copy
    #include"Student.h"
  1. #include"cocos2d.h"
  2. Student::Student(void)
  3. {
  4. }
  5. Student::~Student(void)
  6. cocos2d::CCLog("deletedata");
  7. Student::Student(std::stringname,std::stringsex)
  8. this->name=name;
  9. this->age=age;
  10. this->sex=sex;
  11. }
copy
    #include"Student.h"
  1. #include"cocos2d.h"
  2. Student::Student(void)
  3. {
  4. }
  5. Student::~Student(void)
  6. cocos2d::CCLog("deletedata");
  7. Student::Student(std::stringname,std::stringsex)
  8. this->name=name;
  9. this->age=age;
  10. this->sex=sex;
  11. }
在退出菜单的回调函数中启动两个线程:

copy
    voidHelloWorld::menuCloseCallback(CCObject*pSender)
  1. Student*temp=newStudent(std::string("zhycheng"),23,std::string("male"));
  2. pthread_mutex_init(&mutex,NULL);
  3. pthread_create(&pidrun,NULL,th_run,temp);//启动线程
  4. pthread_create(&pidgo,th_go,0);
  5. }
copy
    voidHelloWorld::menuCloseCallback(CCObject*pSender)
  1. Student*temp=newStudent(std::string("zhycheng"),std::string("male"));
  2. pthread_mutex_init(&mutex,NULL);
  3. pthread_create(&pidrun,temp);//启动线程
  4. pthread_create(&pidgo,0);
  5. }

可以看到,将Student的指针传递给了pidrun线程,那么在pidrun线程中获得Student信息如下:

copy
    Student*s=(Student*)(r);
  1. CCLog("nameis%s,andageis%d,sexis%s",s->name.c_str(),s->age,s->sex.c_str());
  2. deletes;
copy
    Student*s=(Student*)(r);
  1. CCLog("nameis%s,sexis%s",s->sex.c_str());
  2. deletes;

3.线程同步

使用了线程,必然就要考虑到线程同步,不同的线程同时访问资源的话,访问的顺序是不可预知的,会造成不可预知的结果。

这里使用pthread_mutex_t来实现同步,下面我来演示一下使用多线程实现卖票系统。卖票的时候,是由多个窗口同时卖票,这里要做到一张票不要卖出去两次,不要出现有票却无法卖的结果。

在线程函数th_run和th_go中来卖票,票的数量是一个全局变量,每卖出去一张票,就将票的数量减一。其中同步的pthread_mutex_t也是一个全局变量,就用它来实现线程同步。

copy
    void*HelloWorld::th_run(void*r)
  1. Student*s=(Student*)(r);
  2. CCLog("nameis%s,s->sex.c_str());
  3. deletes;
  4. while(true)
  5. pthread_mutex_lock(&mutex);
  6. if(ticket>0)
  7. CCLog("threadrunsell%d",ticket);
  8. ticket--;
  9. pthread_mutex_unlock(&mutex);
  10. }
  11. else
  12. break;
  13. Sleep(1);
  14. //Usleep(10);
  15. returnNULL;
  16. copy
      void*HelloWorld::th_run(void*r)
    1. Student*s=(Student*)(r);
    2. CCLog("nameis%s,s->sex.c_str());
    3. deletes;
    4. while(true)
    5. pthread_mutex_lock(&mutex);
    6. if(ticket>0)
    7. CCLog("threadrunsell%d",ticket);
    8. ticket--;
    9. pthread_mutex_unlock(&mutex);
    10. }
    11. else
    12. break;
    13. Sleep(1);
    14. //Usleep(10);
    15. returnNULL;
    16. }

    来自:http://blog.csdn.net/kaitiren/article/details/14453313

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读