Although the content is copied over. But after my verification. And it's a lot clearer to put together, and the COCOS2DX version number often changes very much. It always leads to problems like that.
in cocos2dx2.01. Header files
#include <pthread.h> pthread_t serial_thread_id; This name is intended to be used on the socket int serialthreadstart (void);//The method to start the thread static void* serialreceiverfun (void *arg);// The thread function that is started, note that it must be a static method ...
2.cpp file
... int helloworld::serialthreadstart () {int errcode=0; Do { pthread_attr_t tattr; Errcode=pthread_attr_init (&tattr); Cc_break_if (errcode!=0); Errcode=pthread_attr_setdetachstate (&tattr, pthread_create_detached); if (errcode!=0) { Pthread_attr_destroy (&tattr); break; } Errcode=pthread_create (&serial_thread_id, &tattr, Serialreceiverfun, this); Cclogerror ("serial_thread_id=%d\n", &serial_thread_id); } while (0); return Errcode;} void* helloworld::serialreceiverfun (void *arg) {cclogerror ("Serial thread Start"); while (true) { char buff[ BUFSIZE]={0}; int readsize = 0; ReadSize = Receiverdate (buff,bufsize); if (readsize > 0) { cclogerror ("readsize=%d,%s\n", Readsize,buff); Senddate (buff,readsize); } } return NULL; ...
3. Turn on threads
This->serialthreadstart ();
cocos2dx3.1 Creating ThreadsIn the COCOS2DX 2.0 era, we were using the Pthread library. is a set of user-level line libraries, widely used in cross-platform applications.
However, no pthread support files were found in COCOS2DX 3.0. The original c++11 already has a better use of the class Std::thread for threading operations. The COCOS2DX 3.0 version number defaults to the vs2012 version number, which supports the new features of c++11, and the use of Std::thread to create threads is simply convenient.
#include <thread>...bool helloworld::init () { if (! Layer::init ()) { return false; } std::thread T1 (&helloworld::mythread,this);//Create a branch thread, Callback into the Mythread function//T1.join (); T1.detach (); Cclog ("in major thread");//return True on the main thread ;} void Helloworld::mythread () {Cclog ("in my Thread");}
T.join () waits for the child thread mythread to finish running, the main thread is able to continue running. At this point the main thread frees up the child thread resource after it has run out. As can be seen from the above picture, it is the first output "in my thread", and then output "in major thread".
of course,Assuming that you do not want to wait on a child thread, you can run T1.detach () inside the main thread to detach the child thread from the main thread and then release the resource itself when the child thread finishes running.
The main thread will have no control over the disconnected threads.
Yes, of course. You can also wear a number of parameters to the thread function, where bind is used.
The following example, when instantiating a thread object, is followed by a two-argument pass after the Mythread function.
#include <thread>bool helloworld::init () { if (! Layer::init ()) { return false; } Std::thread T1 (&helloworld::mythread,this,10,20);//Create a branch thread, callback to the MyThread function//t1.join (); T1.detach (); Cclog ("in major thread");//return True on the main thread ;} void Helloworld::mythread (int first,int second) {Cclog ("in my thread,first =%d,second =%d", first,second);} BOOL Helloworld::init () { if (! Layer::init ()) { return false; } std::thread T1 (&helloworld::mythread,this,10,20);//Create a branch thread. Callback into the Mythread function//t1.join (); T1.detach (); Cclog ("in major thread");//return True on the main thread ;} void Helloworld::mythread (int first,int second) {Cclog ("in my thread,first =%d,second =%d", first,second);}
cocos2dx2.0 and cocos2dx3.1 a summary of different ways to create threads