Cocos2d-x 中使用多線程

原文章在http://blog.csdn.net/huutu/article/details/9889447

一直以爲Cocos2d-x中只有事件隊列,只有一個主線程。。。


首先設置頭文件

  1. E:\Cocos2d-X\cocos2d-2.1rc0-x-2.1.2\cocos2dx\platform\third_party\win32\pthread  

然後添加Lib

[html] view plaincopy
  1. pthreadVCE2.lib  


相關的函數:

  1. pthread_mutex_init(&mutex,NULL);  

理解爲初始化互斥鎖吧。。

  1. pthread_create(&pidgo,NULL,thread_go,0);  

創建一個線程,第一個參數是線程ID,第二個描述爲空,第三個是線程函數,在這個函數裏寫需要在線程裏執行的事件,第四個是傳遞參數給函數。


  1. pthread_mutex_lock(&mutex);  

在一個線程中操作資源的時候就先把資源鎖起來,以免其他線程使用。


  1. pthread_mutex_unlock(&mutex);  

線程中使用完畢後就可以把資源釋放了。這樣別的線程就可以使用資源。



下面是源代碼:

HelloWorldScene.h


  1. #pragma once  
  2.   
  3. #include "cocos2d.h"  
  4.   
  5. #include "Box2D/Box2D.h"  
  6.   
  7. #include "SimpleAudioEngine.h"  
  8.   
  9. #include "pthread.h"  
  10.   
  11. #include<string>  
  12.   
  13. class HelloWorld : public cocos2d::CCLayer  
  14. {  
  15. public:  
  16.     // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone  
  17.     virtual bool init();    
  18.   
  19.     // there's no 'id' in cpp, so we recommand to return the exactly class pointer  
  20.     static cocos2d::CCScene* scene();  
  21.       
  22.     // a selector callback  
  23.     void menuCloseCallback(CCObject* pSender);  
  24.   
  25.     // implement the "static node()" method manually  
  26.     CREATE_FUNC(HelloWorld);  
  27.   
  28.     //Create two function and two tag of thread  
  29.     pthread_t pidrun,pidgo;  
  30.     static void* thread_run(void *r);  
  31.     static void* thread_go(void* r);  
  32.   
  33. };  
  34.   
  35.   
  36. class Student  
  37. {  
  38. public :  
  39.     Student(void){};  
  40.     Student(std::string name,int age,std::string sex)  
  41.     {  
  42.         this->m_age=age;  
  43.         this->m_name=name;  
  44.         this->m_sex=sex;  
  45.     }  
  46.     ~Student()  
  47.     {  
  48.         cocos2d::CCLog("delete data");  
  49.     }  
  50.   
  51.     std::string m_name;  
  52.     std::string m_sex;  
  53.     int m_age;  
  54. };  


HelloWorldScene.cpp


  1. #include "HelloWorldScene.h"  
  2.   
  3. #include<windows.h>  
  4.   
  5.   
  6. using namespace cocos2d;  
  7.   
  8. static int ticket=100;  
  9. static pthread_mutex_t mutex;  
  10.   
  11. CCScene* HelloWorld::scene()  
  12. {  
  13.     CCScene * scene = NULL;  
  14.     do   
  15.     {  
  16.         // 'scene' is an autorelease object  
  17.         scene = CCScene::create();  
  18.         CC_BREAK_IF(! scene);  
  19.   
  20.         // 'layer' is an autorelease object  
  21.         HelloWorld *layer = HelloWorld::create();  
  22.         CC_BREAK_IF(! layer);  
  23.   
  24.         // add layer as a child to scene  
  25.         scene->addChild(layer);  
  26.     } while (0);  
  27.   
  28.     // return the scene  
  29.     return scene;  
  30. }  
  31.   
  32. // on "init" you need to initialize your instance  
  33. bool HelloWorld::init()  
  34. {  
  35.     bool bRet = false;  
  36.     do   
  37.     {  
  38.         //////////////////////////////////////////////////////////////////////////  
  39.         // super init first  
  40.         //////////////////////////////////////////////////////////////////////////  
  41.   
  42.         CC_BREAK_IF(! CCLayer::init());  
  43.   
  44.         //////////////////////////////////////////////////////////////////////////  
  45.         // add your codes below...  
  46.         //////////////////////////////////////////////////////////////////////////  
  47.   
  48.         // 1. Add a menu item with "X" image, which is clicked to quit the program.  
  49.   
  50.         // Create a "close" menu item with close icon, it's an auto release object.  
  51.         CCMenuItemImage *pCloseItem = CCMenuItemImage::create(  
  52.             "CloseNormal.png",  
  53.             "CloseSelected.png",  
  54.             this,  
  55.             menu_selector(HelloWorld::menuCloseCallback));  
  56.         CC_BREAK_IF(! pCloseItem);  
  57.   
  58.         // Place the menu item bottom-right conner.  
  59.         pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));  
  60.   
  61.         // Create a menu with the "close" menu item, it's an auto release object.  
  62.         CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);  
  63.         pMenu->setPosition(CCPointZero);  
  64.         CC_BREAK_IF(! pMenu);  
  65.   
  66.         // Add the menu to HelloWorld layer as a child layer.  
  67.         this->addChild(pMenu, 1);  
  68.   
  69.         // 2. Add a label shows "Hello World".  
  70.   
  71.         // Create a label and initialize with string "Hello World".  
  72.         CCLabelTTF* pLabel = CCLabelTTF::create("Hello World""Arial", 24);  
  73.         CC_BREAK_IF(! pLabel);  
  74.   
  75.         // Get window size and place the label upper.   
  76.         CCSize size = CCDirector::sharedDirector()->getWinSize();  
  77.         pLabel->setPosition(ccp(size.width / 2, size.height - 50));  
  78.   
  79.         // Add the label to HelloWorld layer as a child layer.  
  80.         this->addChild(pLabel, 1);  
  81.   
  82.         // 3. Add add a splash screen, show the cocos2d splash image.  
  83.         CCSprite* pSprite = CCSprite::create("HelloWorld.png");  
  84.         CC_BREAK_IF(! pSprite);  
  85.   
  86.         // Place the sprite on the center of the screen  
  87.         pSprite->setPosition(ccp(size.width/2, size.height/2));  
  88.   
  89.         // Add the sprite to HelloWorld layer as a child layer.  
  90.         this->addChild(pSprite, 0);  
  91.   
  92.         bRet = true;  
  93.     } while (0);  
  94.   
  95.     return bRet;  
  96. }  
  97.   
  98. void HelloWorld::menuCloseCallback(CCObject* pSender)  
  99. {  
  100.     // "close" menu item clicked  
  101.     //CCDirector::sharedDirector()->end();  
  102.   
  103.     Student *temp=new Student(std::string("ThisisGame"),22,std::string("Boy"));  
  104.     pthread_mutex_init(&mutex,NULL);  
  105.     pthread_create(&pidrun,NULL,thread_run,temp);  
  106.     pthread_create(&pidgo,NULL,thread_go,0);  
  107. }  
  108.   
  109.   
  110. void* HelloWorld::thread_run(void* r)  
  111. {  
  112.     Student* s=(Student*)(r);  
  113.     CCLog("Student age=%d,name=%s,sex=%s",s->m_age,s->m_name.c_str(),s->m_sex.c_str());  
  114.     //delete s;  
  115.     while (true)  
  116.     {  
  117.         pthread_mutex_lock(&mutex);  
  118.         if(ticket>0)  
  119.         {  
  120.             CCLog("thread run sell %d",ticket);  
  121.             ticket--;  
  122.             pthread_mutex_unlock(&mutex);  
  123.         }  
  124.         else  
  125.         {  
  126.             pthread_mutex_unlock(&mutex);  
  127.             return NULL;  
  128.         }  
  129. #ifdef WIN32  
  130.         Sleep(1000);  
  131. #else  
  132.         Usleep(1);  
  133. #endif  
  134.     }  
  135.     return NULL;  
  136. }  
  137.   
  138.   
  139. void* HelloWorld::thread_go(void* r)  
  140. {  
  141.     while (true)  
  142.     {  
  143.         pthread_mutex_lock(&mutex);  
  144.         if(ticket>0)  
  145.         {  
  146.             CCLog("thread go sell %d",ticket);  
  147.             ticket--;  
  148.             pthread_mutex_unlock(&mutex);  
  149.         }  
  150.         else  
  151.         {  
  152.             pthread_mutex_unlock(&mutex);  
  153.             return NULL;  
  154.         }  
  155. #ifdef WIN32  
  156.         Sleep(1000);  
  157. #else  
  158.         Usleep(1);  
  159. #endif  
  160.     }  
  161.     return NULL;  
  162. }  


程序運行結果:






然後是工程打包:

[html] view plaincopy
  1. http://download.csdn.net/detail/cp790621656/5905699  
發佈了53 篇原創文章 · 獲贊 4 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章