Multi-thread in cocos2d-x3.2
In fact, the use of multithreading in cocos2dx is actually written in C ++. Here are a few simple examples:
1,
//// PublicScene. cpp // testthirdone /// Created by du jia on 14-7-1. // void hello () {log (hello thread);} bool PublicScene: init () {bool bRet = false; do {CC_BREAK_IF (! Scene: init (); std: thread t1 (hello); t1.join (); log (main thread); bRet = true;} while (0 ); return bRet;} std: thread t1 (hello); t1.join (); log (main thread );
2,
//// PublicScene. cpp // testthirdone /// Created by du jia on 14-7-1. /// void hello () {log (hello thread);} bool PublicScene: init () {bool bRet = false; do {CC_BREAK_IF (! Scene: init (); std: vector
Threads; for (int I = 0; I <5; ++ I) {threads. push_back (std: thread ([=] () {log (% s, StringUtils: format (thread % d, I ). c_str () ;}) ;}for (auto & thread: threads) {thread. join ();} log (Main Thread); bRet = true;} while (0); return bRet ;}
3,
//// PublicScene. cpp // testthirdone /// Created by du jia on 14-7-1. /// void hello () {log (hello thread);} bool PublicScene: init () {bool bRet = false; do {CC_BREAK_IF (! Scene: init (); std: mutex m; std: thread t1 ([& m] () {m. lock (); for (int I = 0; I <10; I ++) {log (% s, StringUtils: format (thread1% d, I ). c_str ();} m. unlock () ;}); std: thread t2 ([& m] () {m. lock (); for (int I = 0; I <10; I ++) {log (% s, StringUtils: format (thread2% d, I ). c_str ();} m. unlock () ;}); t1.join (); t2.join (); log (Main Thread); bRet = true ;}while (0); return bRet ;}