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

cocos2dx多线程以及线程同步 与 cocos2dx内存管理与多线程问题,第1张

概述ocos2d-x引擎在内部实现了一个庞大的主循环,每帧之间更新界面,如果耗时的 *** 作放到了主线程中,游戏的界面就会卡,这是不能容忍的,游戏最基本的条件就是流畅性,这就是为什么游戏开发选择C++的原因。另外现在双核手机和四核手机越来越普遍了,是时候使用多线程来挖掘硬件的潜力了。 1.环境搭建 cocos2d-x中的多线程使用pthread就可以实现跨平台,而且也不是很难理解。使用pthread需要先配 ocos2d-x引擎在内部实现了一个庞大的主循环,每帧之间更新界面,如果耗时的 *** 作放到了主线程中,游戏的界面就会卡,这是不能容忍的,游戏最基本的条件就是流畅性,这就是为什么游戏开发选择C++的原因。另外现在双核手机和四核手机越来越普遍了,是时候使用多线程来挖掘硬件的潜力了。 1.环境搭建

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


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


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

2.多线程的使用

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

[cpp] view plain copy PTW32_DLLPORTintPTW32_CDECLpthread_create(pthread_t*tID,//线程的标示 constpthread_attr_t*attr,//创建线程的参数 voID*(*start)(voID*),//入口函数的指针 voID*arg);//传递给线程的数据 [cpp] view plain copy PTW32_DLLPORTintPTW32_CDECLpthread_create(pthread_t*tID,//线程的标示 constpthread_attr_t*attr,//创建线程的参数 voID*(*start)(voID*),//入口函数的指针 voID*arg);//传递给线程的数据
在HelloWorldScene.h文件中

copy pthread_tpIDrun,pIDgo; staticvoID*th_run(voID*r); staticvoID*th_go(voID*r); copy pthread_tpIDrun,pIDgo; staticvoID*th_run(voID*r); staticvoID*th_go(voID*r); 定义了两个函数和两个线程的标识。

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

copy #pragmaonce #include<string> classStudent { public: Student(voID); Student(std::stringname,intage,std::stringsex); ~Student(voID); std::stringname; intage; std::stringsex; }; copy #pragmaonce #include<string> classStudent { public: Student(voID); Student(std::stringname,intage,std::stringsex); ~Student(voID); std::stringname; intage; std::stringsex; };
源文件如下

copy #include"Student.h" #include"cocos2d.h" Student::Student(voID) { } Student::~Student(voID) cocos2d::cclog("deletedata"); Student::Student(std::stringname,std::stringsex) this->name=name; this->age=age; this->sex=sex; } copy #include"Student.h" #include"cocos2d.h" Student::Student(voID) { } Student::~Student(voID) cocos2d::cclog("deletedata"); Student::Student(std::stringname,std::stringsex) this->name=name; this->age=age; this->sex=sex; } 在退出菜单的回调函数中启动两个线程:

copy voIDHelloWorld::menuCloseCallback(CCObject*pSender) Student*temp=newStudent(std::string("zhycheng"),23,std::string("male")); pthread_mutex_init(&mutex,NulL); pthread_create(&pIDrun,NulL,th_run,temp);//启动线程 pthread_create(&pIDgo,th_go,0); } copy voIDHelloWorld::menuCloseCallback(CCObject*pSender) Student*temp=newStudent(std::string("zhycheng"),std::string("male")); pthread_mutex_init(&mutex,NulL); pthread_create(&pIDrun,temp);//启动线程 pthread_create(&pIDgo,0); }
可以看到,将Student的指针传递给了pIDrun线程,那么在pIDrun线程中获得Student信息如下:

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

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

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

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

copy voID*HelloWorld::th_run(voID*r) Student*s=(Student*)(r); cclog("nameis%s,s->sex.c_str()); deletes; while(true) pthread_mutex_lock(&mutex); if(ticket>0) cclog("threadrunsell%d",ticket); ticket--; pthread_mutex_unlock(&mutex); } else break; Sleep(1); //Usleep(10); returnNulL; copy voID*HelloWorld::th_run(voID*r) Student*s=(Student*)(r); cclog("nameis%s,s->sex.c_str()); deletes; while(true) pthread_mutex_lock(&mutex); if(ticket>0) cclog("threadrunsell%d",ticket); ticket--; pthread_mutex_unlock(&mutex); } else break; Sleep(1); //Usleep(10); returnNulL; }

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

总结

以上是内存溢出为你收集整理的cocos2dx多线程以及线程同步 与 cocos2dx内存管理与多线程问题全部内容,希望文章能够帮你解决cocos2dx多线程以及线程同步 与 cocos2dx内存管理与多线程问题所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1081334.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存