COCOS2DX Center Thread Priority

Source: Internet
Author: User

COCOS2DX Thread Priority problem

Whether it's iOS or Android, the time-consuming tasks have to be threaded, otherwise the program will not be able to prompt users for feedback. In the game if a lap cycle can not be completed in the 1/framerate (frame rate is 30 1/30) seconds, there will be dropped frames, game lag. For example, in the game to parse an animation JSON configuration, need 60ms, this if in the main thread to drop the frame, then another thread to parse the JSON, the parsing is finished and then notify the main thread? This is possible, but consider an extreme case where the thread switches to the JSON parsing thread until the JSON parsing is complete before switching to the main thread, which is not the same as dropping frames! so the ideal situation is to let the JSON parsing thread priority lower than the main thread, JSON parsing when the main thread is idle, which is the priority of the control threads, can I set the thread priority in COCOS2DX? Please see below.

1. Create threading Mode

The creation thread takes the thread library in c++11, but the thread in c++11 does not specify a method for threading priority or CPU affinity (affinity) (such as Sched_setaffinity, Setthreadaffinitymask, Pthread_attr_setschedpolicy). It is possible to control thread priority by using a function in the Pthread library (currently using Pthread_setschedparam).

2. Thread Priority:

Sched_other, Sched_fifo, SCHED_RR, and other Sched_batch, Sched_batch and sched_idle are commonly used in the scheduling strategy of the middle thread priority of Unix-like systems. Refer to Sched_setscheduler (2)-Linux man page.

Sched_other: The newly created thread is this dispatch by default , and the main thread is this scheduling mode, which has no thread priority, and is fairly dispatched by the operating system when all threads are Sched_other-Dispatch mode.

Sched_fifo and SCHED_RR: There are priority schedules , high priority threads, and operating systems running high-priority threads. When both Sched_other and Sched_fifo (or SCHED_RR) are available,Sched_fifo (or SCHED_RR) always runs before Sched_other .

The difference between Sched_fifo and SCHED_RR is that, if the thread priority is the same, the Sched_fifo runs in the FIFO order, and the incoming thread waits until the previous thread finishes running or discards the CPU to execute. The SCHED_RR will rotate the time slice, the time slice is up, switch the execution thread.

The advantage of Sched_other is that the CPU utilization is higher than sched_fifo and SCHED_RR without the real-time (high priority thread switching thread) switch on the processing thread.

You can refer to the Linux process scheduling method (SCHED_OTHER,SCHED_FIFO,SCHED_RR)

3. Conclusion:

  If the default scheduling rule of the main thread is not changed sched_other, the thread priority is set through pthread_setschedparm without effect, that is, there is no guarantee that other threads will not affect the execution efficiency of the main thread. If you change the schedule rule of the main thread to Sched_fifo (or SCHED_RR), the created thread also sets the schedule rule to Sched_fifo and sets the low priority, which guarantees a higher CPU usage for the main thread.

Add the following code in the appdelegate::applicationdidfinishlaunching change the main thread scheduling mode, the game can run normally, side effects are not found.

4. test Data

The maximum minimum value of the thread priority priority for a print is 47 to 15, not the legendary 1 to 99. Sched_other Max is a, Min is 15. Sched_fifo Max is a, Min is 15. SCHED_RR Max is a, Min is 15. Each test opens 2 threads (thread 1 and Thread 2), sets the scheduling mode and priority respectively, and the Scarlet Letter is a confusing place.

Mac Simulator

Dispatch mode

Priority level

Log Print Count

Thread 1

Sched_other

46

16374

Thread 2

Sched_other

16

311

Thread 1

Sched_other

16

579

Thread 2

Sched_other

46

7856

Thread 1

Sched_other

30

1400

Thread 2

Sched_other

31

8082

Thread 1

Sched_other

31

8628

Thread 2

Sched_other

30

1864

Thread 1

Sched_rr

16

1099

Thread 2

Sched_other

46

12784

iOS simulator under Mac

Dispatch mode

Priority level

Log Print Count

Thread 1

Sched_other

46

16799

Thread 2

Sched_other

16

238

Thread 1

Sched_other

16

3982

Thread 2

Sched_other

46

103177

Thread 1

Sched_other

30

2147

Thread 2

Sched_other

31

21478

Thread 1

Sched_rr

16

1674

Thread 2

Sched_other

46

16096

iphone 5s:

Dispatch mode

Priority level

Log Print Count

Thread 1

Sched_other

46

463/1465/1553

Thread 2

Sched_other

16

1781/1726/1568

Thread 1

Sched_other

16

1030

Thread 2

Sched_other

46

1284

Thread 1

Sched_other

30

372

Thread 2

Sched_other

31

2005

Thread 1

Sched_rr

16

52262

Thread 2

Sched_other

46

1161

Thread 1

Sched_rr

46

13055

Thread 2

Sched_rr

16

1145

Thread 1

Sched_rr

46

19858

Thread 2

Sched_rr

45

2046

Under Mac App

Dispatch mode

Priority level

Log Print Count

Thread 1

Sched_other

46

13099

Thread 2

Sched_other

16

59311

Thread 1

Sched_other

16

45864

Thread 2

Sched_other

46

3598

Thread 1

Sched_other

30

7512

Thread 2

Sched_other

31

4561

Thread 1

Sched_rr

16

57981

Thread 2

Sched_other

46

5909

Thread 1

Sched_rr

46

58960

Thread 2

Sched_rr

45

5494

Test the code for Mac app:

COCOS2DX Test Code:

COCOS2DX Center Thread Priority

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.