运行 如何在 cocos2d-x 的另一个线程中自定义网络代码?

How to run custom network code in another thread in cocos2d-x?

如何在cocos2d-x场景的另一个线程中运行自定义网络代码?我正在 cocos2d-x 和 SDL_Net.

上制作回合制多人游戏

使用std::thread创建和分离第二个线程:

#include <chrono>
#include <thread>
#include <iostream>

int main(int argc, char *argv[])
{
    std::thread thread_name([]()
    {
        while (true)
        {
            std::this_thread::sleep_for(std::chrono::seconds(2));
            std::cout << "Hello from other thread" << std::endl;
        }
    });
    thread_name.detach();
    std::this_thread::sleep_for(std::chrono::seconds(11));
    return 0;
}

使用 cocos2d::Scheduler 安排来自该线程的操作:

cocos2d::Director::getInstance()->getScheduler()->schedule([](float)
{
    std::cout << "We can operate cocos-related things here" << std::endl;
}, this, 0.0f, 1, 0, false, "name");