码迷,mamicode.com
首页 > 编程语言 > 详细

cocos2d-x3.2 下使用多线程

时间:2014-07-02 09:19:13      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:cocos2dx   多线程   c++   thread   

其实在cocos2dx下使用多线程其实就是用C++去写,这里提供几个简单的例子:

原文地址:http://blog.csdn.net/qqmcy/article/details/36227377

1、

//
//  PublicScene.cpp
//  testthirdone
//
//  Created by 杜甲 on 14-7-1.
//


void hello()
{
    log("hello thread");
}

bool PublicScene::init()
{
    bool bRet = false;
    do {
        CC_BREAK_IF(!Scene::init());
		
		
        std::thread t1(hello);
        t1.join();
        log("主线程");
		
		
		  bRet = true;
	  } while (0);
	  return bRet;
}



        std::thread t1(hello);
        t1.join();
        log("主线程");

2、

//
//  PublicScene.cpp
//  testthirdone
//
//  Created by 杜甲 on 14-7-1.
//
//

void hello()
{
    log("hello thread");
}

bool PublicScene::init()
{
    bool bRet = false;
    do {
        CC_BREAK_IF(!Scene::init());
		
		
        std::vector<std::thread> threads;
        for (int i = 0; i < 5; ++i)
        {
            threads.push_back(std::thread([ = ]()
            {
                log("%s",StringUtils::format(" thread %d",i).c_str());
            }));
        }
        
        for (auto& thread :threads) {
            thread.join();
        }
        
        log("Main Thread");

		
		
		  bRet = true;
	  } while (0);
	  return bRet;
}

3、

//
//  PublicScene.cpp
//  testthirdone
//
//  Created by 杜甲 on 14-7-1.
//
//

void hello()
{
    log("hello thread");
}

bool PublicScene::init()
{
    bool bRet = false;
    do {
        CC_BREAK_IF(!Scene::init());
		
		
        std::mutex m;
        std::thread t1([&m](){
            m.lock();
            for (int i = 0; i < 10; i++) {
                
                log("%s",StringUtils::format(" thread1 %d",i).c_str());

               
            }
             m.unlock();
        });
        
        
        std::thread t2([&m](){
        
            m.lock();
            for (int i = 0; i < 10; i++) {
                
                
                log("%s",StringUtils::format(" thread2 %d",i).c_str());
                

               
            }
             m.unlock();
        });
        
        t1.join();
        t2.join();
        
        log("Main Thread");
		
		
		  bRet = true;
	  } while (0);
	  return bRet;
}



cocos2d-x3.2 下使用多线程,布布扣,bubuko.com

cocos2d-x3.2 下使用多线程

标签:cocos2dx   多线程   c++   thread   

原文地址:http://blog.csdn.net/qqmcy/article/details/36227377

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!