How to use std::thread in cocos2d-x 3.0?

hi, can std::thread(c++11) replace pthread in cocos2d-x 3.0?

anybody know that?

I’m not sure about 3.0 but in 2.x versions it is not recommended to use threading as the engine is not thread safe. But if you still want to use threading then yes use the C++11 std::thread.

If you do a search in your cocos2dx project for pthread you can find this line: “[Refactor] #2305: Use std::thread instead of pthread” from the first release alpha of cocos2dx version 3. And if you search for std::thread - its all over cocos files, so i am assuming they have changed it to std

a simple example:

#include <thread>

static const int num_threads = 10;

//This function will be called from a thread

void call_from_thread(int tid) {
    std::cout << "Launched by thread " << tid << std::endl;
}

int main() {
    std::thread t[num_threads];

    //Launch a group of threads
    for (int i = 0; i < num_threads; ++i) {
        t[i] = std::thread(call_from_thread, i);
    }

    std::cout << "Launched from the main\n";

    //Join the threads with the main thread
    for (int i = 0; i < num_threads; ++i) {
        t[i].join();
    }

    return 0;
}

Check out Solarian Programmer: http://solarianprogrammer.com/2011/12/16/cpp-11-thread-tutorial/