c++11,cocos2d 3.x多線程std::thread demo

std::thread
 1. 在cocos2dx中,子線程不能用於更新主界面,更新界面是主線程的事
 2. 在cocos2dx中,只有 
Director::getInstance()->getScheduler()->performFunctionInCocosThread

方法可以調用

//
//  OneScene.cpp
//  helloworld
//
//  Created by liqiangxo on 17/3/4.
//
//
#include "OneScene.hpp"
#include "unistd.h"
#include "Onebody.hpp"
OneScene* OneScene::getInstance(){
    static OneScene* _instance = nullptr;
    if (_instance == nullptr) {
        _instance = new OneScene;
        _instance->init();
    }
    return _instance;
}
bool OneScene::init(){
    tickets = 1000;//1000張票

    std::thread tA(&OneScene::myThreadA,this);//創建一個分支線程,回調到myThread函數裏
    std::thread tB(&OneScene::myThreadB,this);
    tA.detach();
    tB.detach();
    //  t1.detach();
    CCLOG("in major thread");//在主線程
    return true;
}

void OneScene::setSpriteName1(Texture2D * filename){
    bodufilename1 = filename;
}
void OneScene::setSpriteName2(Texture2D * filename){
    bodufilename2 = filename;
}
void addBody1(){

    auto scene = Director::getInstance()->getRunningScene();
    auto sp = Sprite::createWithTexture(OneScene::getInstance()->bodufilename1);
    sp->setPosition(Vec2(rand_0_1()*Director::getInstance()->getWinSize().width ,rand_0_1()*Director::getInstance()->getWinSize().height));
    scene->addChild(sp,1000);
}
void addBody2(){

    auto scene = Director::getInstance()->getRunningScene();
    auto sp = Sprite::createWithTexture(OneScene::getInstance()->bodufilename2);
    sp->setPosition(Vec2(rand_0_1()*Director::getInstance()->getWinSize().width ,rand_0_1()*Director::getInstance()->getWinSize().height));
    scene->addChild(sp,1000);
}

void OneScene::myThreadA()
{
    while(true)
    {
        mutexa.lock();//加鎖
        if(tickets>0)
        {
            if(tickets == 1000){
                sleep(5);
            }
            sleep(1);
            Director::getInstance()->getScheduler()->performFunctionInCocosThread(addBody1);
            CCLOG("A Sell %d",tickets--);//輸出售票,每次減1
            mutexa.unlock();//解鎖
        }
        else {
            mutexa.unlock();
            break;

        }
    }
}
void OneScene::myThreadB()
{
    while(true)
    {
        mutexa.lock();
        if (tickets>0)
        {
            if(tickets == 1000){
                sleep(5);
            }
            sleep(1);

            Director::getInstance()->getScheduler()->performFunctionInCocosThread(addBody2);
            CCLOG("B Sell %d",tickets--);
            mutexa.unlock();
        }
        else
        {
            mutexa.unlock();
            break;
        }
    }
}
//
//  OneScene.hpp
//  helloworld
//
//  Created by liqiangxo on 17/3/4.
//
//

#pragma once
#ifndef OneScene_hpp
#define OneScene_hpp

#include <stdio.h>
#include "cocos2d.h"
USING_NS_CC;

class OneScene {
public:
    static OneScene* getInstance();
    virtual bool init();

    void myThreadA();//線程A
    void myThreadB();//線程B
    Texture2D * bodufilename1;
    void setSpriteName1(Texture2D * filename);
    Texture2D * bodufilename2;
    void setSpriteName2(Texture2D * filename);
    std::mutex  mutexa;
    int tickets;//票數
};
#endif /* OneScene_hpp */
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章