Http://www.cocoachina.com/cocos/20140523/8530.html
Simple use of "Cocos2d-x 3.0" c++11 multithreaded Std::thread2014-05-23 17:01 Edit: Wupeng Category: Cocos engine Source: Cocoachina 08773
Cocos2d-x v3.0 Multithreading c++11
Recruitment information:
- Cocos2d-x front-end main thread
- iOS Development engineer of Hangzhou NetEase
- Senior Java Engineer
- IOS Creative Engineer
- Find an iOS siege lion
- iOS Senior Development Engineer
- Advanced iOS Development Engineer
- Mobile internet company-ios Senior Engineer
- iOS Development Engineer
- iOS Development Engineer
- iOS Advanced Development Engineer (quick strokes)
The blog from Tianya Horizon
This article is about threading! In the Cocos2d-x 2.0 era, we used the Pthread library, a set of user-level line libraries, widely used in cross-platform applications. However, in Cocos2d-x 3.0 did not find a pthread supporting documents, the original
C++11 already has a better class for threading operations Std::thread。 The version of Cocos2d-x 3.0 defaults to the vs2012 version, which supports the new features of c++11, and it is simply convenient to use Std::thread to create threads. Here is a brief introduction to the following Std::thread, code
need to include header files
- 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"); In the main thread
- return true;
- }
- void Helloworld::mythread ()
- {
- Cclog ("in my Thread");
- }
Running results such as:
T.join () waits for the mythread of the child thread to execute, the main thread can continue execution, and the main thread frees the child thread resources after execution. As can be seen from the above picture, it is the first output "in my thread", and then output "in major thread". Of course
If you do not want to wait for a child thread, you can execute T1.detach () inside the main thread to detach the child thread from the main thread, and the child thread will release the resource itself when it finishes executing. The main thread will have no control over the disconnected threads. As follows:
- Std::thread T1 (&helloworld::mythread, this); Create a branch thread, callback into the Mythread function
- T1.detach ();
The results of the operation are as follows: Of course, you can also wear parameters to the thread function, which is used here.
Bind。 In the following example, when instantiating a threading object, the thread function Mythread followed by two parameters.
- 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"); In the main thread
- return true;
- }
- void Helloworld::mythread (int First,int second)
- {
- Cclog ("in my thread,first =%d,second =%d", first,second);
- }
Output results such as: simple things I have said almost, the following practical walkthrough. Please allow me to refer to the occasional e-story of a thread of the blog, he used pthread, here I use Std::thread.
TicketingA total of 100 Noah's Ark ferry tickets, there are 2 ticket points A and b at the ticket ($ 10 billion for a ticket), pawn ticket sold out on the end. We know that when the program starts the process it will create a main thread, so you can create 2 threads A and B on the main thread, then the threads A and B are sold separately, and the pawn ticket number is 0, ending threads A and B. This is accomplished with the std::thread of cocos2d-x3.0 and c++11.
multi-thread ticketing, the code is as follows:
- HelloWorld.h
- Class HelloWorld: Public cocos2d::layer
- {
- Public
- static cocos2d::scene* createscene ();
- virtual BOOL init ();
- Create_func (HelloWorld);
- void Mythreada (); //Thread a
- void mythreadb (); //thread B
- int tickets; //Number of votes
- };
- . cpp
- BOOL Helloworld::init ()
- {
- if (! Layer::init ())
- {
- return false;
- }
- Tickets = 100; //100 Tickets
- Std::thread TA (&helloworld::mythreada, this); Create a branch thread, callback into the Mythread function
- Std::thread TB (&helloworld::mythreadb, this);
- Ta.detach ();
- Tb.detach ();
- T1.detach ();
- Cclog ("in major thread"); In the main thread
- return true;
- }
- void Helloworld::mythreada ()
- {
- While (true)
- {
- if (tickets>0)
- {
- Sleep (10);
- Cclog ("A Sell%d", tickets--); Output ticketing, minus 1 per time
- }
- else {
- Break ;
- }
- }
- }
- void Helloworld::mythreadb ()
- {
- While (true)
- {
- if (tickets>0)
- {
- Sleep (10);
- Cclog ("B Sell%d", tickets--);
- }
- Else
- {
- Break ;
- }
- }
- }
The code is simple, not much to say. We look at the output, we will find that there are a lot of popular phenomenon, because each time the results of each run is different, so the results are not posted here, which is more interesting phenomenon is the same ticket sold two times?! The reason is not much explanation, time slice of the problem, do not understand Google. If you don't think this is going to happen, then add the following sentence before you print the result:
- Sleep (100);
Operation Result:
synchronizing data with mutex objectsThe problem is mainly because when one thread executes halfway, the switch of the time slice causes the other thread to modify the same data, and when the original thread is switched again and continues to run down, the data is changed and the result is faulted. So what we're going to do is make sure that this thread is fully executed, so it's a good idea to line Cheng that the mutex is the lock.
1) Initialize mutex lock
- Std::mutex Mutex; //Thread Mutex object
2) Modify the code of Mythreada and MYTHREADB, add the mutex inside
- void Helloworld::mythreada ()
- {
- While (true)
- {
- Mutex.lock (); //Locking
- if (tickets>0)
- {
- Sleep (10);
- Cclog ("A Sell%d", tickets--); Output ticketing, minus 1 per time
- Mutex.unlock (); //Unlock
- }
- else {
- Mutex.unlock ();
- Break ;
- }
- }
- }
- void Helloworld::mythreadb ()
- {
- While (true)
- {
- Mutex.lock ();
- if (tickets>0)
- {
- Sleep (10);
- Cclog ("B Sell%d", tickets--);
- Mutex.unlock ();
- }
- Else
- {
- Mutex.unlock ();
- Break ;
- }
- }
- }
Run the result as follows, perfect!
Simple use of c++11 multithreading Std::thread