Cocos2d-x中的多线程Demo

前端之家收集整理的这篇文章主要介绍了Cocos2d-x中的多线程Demo前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

.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,后期会有一个完善的,学习中。。。。。。

原文链接:https://www.f2er.com/cocos2dx/346436.html

猜你在找的Cocos2d-x相关文章