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

Cocos2d-x中的多线程简单Demo

时间:2014-11-03 19:24:17      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:cocos2d-x中的多线程   thread   

.h文件

#ifndef __THREADDEMO_H_
#define __THREADDEMO_H_

#include "cocos2d.h"
using namespace cocos2d;

class ThreadDemo:public Layer{
public:
	CREATE_FUNC(ThreadDemo);
	virtual bool init();

	static Scene *createScene();

	void leftHand();
	void rightHand();

};



#endif // !__THREADDEMO_H_

.cpp文件:

#include "ThreadDemo.h"

bool ThreadDemo::init(){
	if (!Layer::init())
	{
		return false;
	}
	//创建、开启线程1
	std::thread threah1 = std::thread(&ThreadDemo::leftHand,this);
	threah1.detach();

	//创建、开启线程2
	std::thread thread2 = std::thread(&ThreadDemo::rightHand,this);
	thread2.detach();



	return true;
}

Scene *ThreadDemo::createScene(){
	Scene *scene  = Scene::create();
	auto layer = ThreadDemo::create();
	scene->addChild(layer);
	return scene;
}

void ThreadDemo::leftHand(){
	log("==========draw left start=============");
	int num = 0;
	while (num < 1000000000)
	{
		num++;

	}

	log("===============draw left end==========");
}
void ThreadDemo::rightHand(){
	int num = 0;
	while (num<4)
	{
#if(CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
			Sleep(1000);//睡眠1秒钟
			log("sleep");
#else
			usleep(1000);
#endif
		log("draw right start");
		num++;
	}
}

这只是一个很简单的Demo,后期会有一个完善的,学习中。。。。。。

bubuko.com,布布扣

Cocos2d-x中的多线程简单Demo

标签:cocos2d-x中的多线程   thread   

原文地址:http://blog.csdn.net/majianjie/article/details/40745505

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