Cocos2d-X多线程(3) cocos2dx中的线程安全

在使用多线程时,总会遇到线程安全的问题。cocos2dx 3.0系列中新加入了一个专门处理线程安全的函数performFunctionInCocosThread(),他是Scheduler类的一个成员函数:
void Scheduler::performFunctionInCocosThread(const std::function<void ()> &function)
当在其他线程中调用cocos2d的函数时使用该函数。
使用这个函数就能安全的在其他线程中去控制cocos2dx的一些操作了。比如在worker线程中对精灵的创建等操作可能会没有用,在performFunctionInCocosThread就能很容易实现。
比如:
void thread_fun()
{
  log("new thread create:t_id:0x%x",GetCurrentThreadId());
 
  Director::getInstance()->getScheduler()->performFunctionInCocosThread([&,this]
  {
   for(int i=0;i<=1000;i++){}
   log("[performFunctionInCocosThread] finished!");
  });
 
  log("thread finished:t_id:0x%x",GetCurrentThreadId());
}
这时会看到一个问题,线程结束后performFunctionInCocosThread中运行的代码才结束,这是因为使用performFunctionInCocosThread将参数函数中的代码放到主线程中去运行,所以就无法知道运行完这段代码需要多少时间。
 
有时还会遇到互斥问题,如果在线程中要使用互斥,而又要使用performFunctionInCocosThread。遇到这种情况将performFunctionInCocosThread调用放到线程的最后,然后在performFunctionInCocosThread调用函数的末尾使用mutex.unlock(),这样才能确保互斥。
 


代码实操:

头文件:

#ifndef __TestThreadSafe_SCENE_H__
#define __TestThreadSafe_SCENE_H__

#include "cocos2d.h"


USING_NS_CC;

class TestThreadSafe : public cocos2d::Layer
{
public:
    static cocos2d::Scene* createScene();
    virtual bool init();
	CREATE_FUNC(TestThreadSafe);
	
	void threadA();
};

#endif // __TestThreadSafe_SCENE_H__

源文件:

#include "TestThreadSafe.h"
#include <thread>

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

bool TestThreadSafe::init()
{
    if ( !Layer::init() )
    {
        return false;
    }

	std::thread t1(&TestThreadSafe::threadA,this);
	t1.detach();

    return true;
}
void TestThreadSafe::threadA()
{
	Director::getInstance()->getScheduler()->performFunctionInCocosThread([&,this]{
		auto sprite = Sprite::create("HelloWorld.png");
		addChild(sprite);
		Size size = Director::getInstance()->getWinSize();
		sprite->setPosition(size/2);
	});
}


 
 

posted @ 2018-09-02 18:29  穆玄琅  阅读(874)  评论(0编辑  收藏  举报