cocos2d-x多线程操作

先头文件定义:

void threadA();

再在.cpp文件中添加

//1.引入头文件#include <thread>
    std::thread t1(&Login::threadA,this);//取Login的地址
    t1.detach();//主线程和子线程互不影响
    //t1.join();//阻塞主线程:执行子线程完了后才执行主线程

    CCLOG("First Thread Id is: %x",this_thread::get_id());
void Login::threadA()
{
    //设置本线程等待5秒
    this_thread::sleep_for(chrono::seconds(5));
    CCLOG("Second Thread Id is: %x", this_thread::get_id());

}



这样就算是起了一个新的线程,和主线程同时存在,这个线程睡眠5秒钟后输出了一个日志。

如果程序中途被关闭新的线程还在继续运行,那么程序关闭的时候会出错。

猜你喜欢

转载自blog.csdn.net/pyf_914406232/article/details/90412125
今日推荐