MMORPG large game design and development (multi-threaded server game scene)

Source: Internet
Author: User

Multithreading in the development of the CPU with more and more, the game scene because of its amount of data on the server and the logical complexity of the reason must depend on it. Why are multiple threads used in a scene? What is the thread of the scene? How is the thread of the scene created? How do the threads of a scene manage? Here at the same time to the wrong friends to say thank you, although it is a small mistake, but also hope that we can be bold to correct these mistakes.

Game

Reasons for adoption

The two above are in different scenarios, just imagine what happens if a thread can only process data from one of the graphs first.

Single-threaded often need to wait, as if we go to the Bank window to transact business, the former time many outlets only one window, so we have to wait in line, but with the progress of the Times the Bank's window with the development of more and more business, more salesman makes us save the queue of trouble. And the game scene in the game to deal with the logic is the most complex, especially some large scenes of data that is more numerous, the process of processing logic data need time, in the single-line thread face complex logic will need to wait a lot of time, the more complex the data and logic its waiting time will be longer, You think if we play the game in the a scene, B scene because to wait for the a scene of the logic processing to be able to process the data of its scene, that is, a scene of the person first walk, the B scene players can move around.
In order to get all the data in the scene to be processed in a short time, we need more handlers to help, that is, we have more threads to handle, if only one thread processing n scenes is of course very slow, if there are m scenes simultaneously processing n scene speed will be greatly improved, That's why we chose multithreading. And the number of scenes a thread can withstand is often related to the size and complexity of the scene's data volume.

Scenario Multithreading 1, Thread Manager (manager)

Used to add scene threads and initialize thread data.

2. Thread pool (pool)

Collection management of multiple thread data.

3. Thread Assignment (threads)

The best thing in the world is to have enough resources, just like here. Each thread is responsible for a scenario that is ideal because it is the highest efficiency. The more threads that are consumed by the CPU naturally increases in positive proportions, as if the resources on Earth are not completely satisfied with the requirements, so that we have to find ways to share resources, one thread responsible for multiple scenarios. Remember as a child of the Labor class, sometimes the tool is not enough, the teacher will often be divided into groups of students, each group to share those tools, but sometimes a tool is divided by a lot of people to use, more than the load when the teacher's requirements (such as the teacher asked for the time required for everyone to use the tool how many times, Because there are too many people, even some tools can not touch.

The load of the thread is mainly manifested in the speed and efficiency of program execution, if the efficiency is obviously very low affect the normal work, we have to find ways to improve efficiency. The most direct impact on the load of the scene thread is responsible for the number of scenes, so the scene thread has the specified scene limit.

General process

Scene Manager creates all scene manager--Thread pool object initialization (thread pool), Thread Manager assignment scenario thread (thread manager), scene thread Manager, running all threads (thread Manager

Algorithm (swap sort) 1, bubble sort (bubble sort)

Swaps the next two elements, moving the largest element gradually to the last, and if the array size is n, the array becomes an ordered array after n times. The bubble sorting algorithm is mainly used for occasions where the ordering element is less and the time requirement is not high. Algorithm improvements are made here: Add a tag, and if there are no elements that need to be swapped, then there is no need to sort them back.

Code.
#include <stdio.h>#include<stdint.h>#include<inttypes.h>/** swap the next two elements, move the largest element gradually to the last, if the array size is n, then the array becomes an ordered array after n times * bubble sorting algorithm is mainly used to sort elements less and time requirements are not too high * algorithm improvement: Add a marker, If there are no elements that need to be swapped, then no further sorting is required*/ voidPrintArray (int32_t array[], int32_t length);voidBubblesort (int32_t array[], int32_t length); int32_t Main (int32_t argc,Char*argv[]) {int32_t array[]= {3, -,8,9, the, $, About, About,102, the}; int32_t length=sizeof(array)/sizeof(array[0]);    Bubblesort (array, length); return 0;}voidPrintArray (int32_t array[], int32_t length) {int32_t i;  for(i =0; i < length; ++i) printf ("%4d", Array[i]); printf ("\ n");}voidBubblesort (int32_t array[], int32_t length) {int32_t I, J;  int32_t temp; uint8_t Flag=1;  for(i =1; I < length &&1= = Flag; ++i) {flag=0;  for(j =0; J < Length-i; ++j) {if(Array[j] > array[j +1]) {temp=Array[j]; ARRAY[J]= Array[j +1]; Array[j+1] =temp; Flag=1; }} printf ("The %d times result:", i);  PrintArray (array, length); }}
Result.

2. Fast sorting (Quick sort)

The fast sorting algorithm is the improvement of the bubbling sorting algorithm, but the implementation is much more complex than bubbling, it mainly for big data sorting, the time requirement is not very high can use direct insertion sort or bubble sort. It is characterized by a high time efficiency of sequencing and is evident in particularly large data volumes. Like the hill algorithm, fast sequencing is a less stable algorithm, which is related to the principle of its implementation.

Code.
#include <stdio.h>#include<stdint.h>#include<inttypes.h>/** * Fast sorting algorithm is the improvement of the bubble sorting algorithm, but the implementation is much more complex than bubbling, it mainly for big data sorting, * if the time requirement is not very high can use direct insertion sort or bubble sort * it is characterized by a high time efficiency of sorting, and in the particularly large amount of data is very obvious. * Like the hill algorithm, fast sequencing is a less stable algorithm, which is related to the principle of its implementation. */ //Array PrintingvoidDisplayarray (int32_t array[], int32_t length);//outputs the result of each partitionvoid_displayarray (int32_t array[], int32_t length, int32_t pivot, int32_t count);//Quick Sort Core implementationvoid_quicksort (int32_t array[], int32_t length, int32_t Low, int32_t high);//Quick sort function, sorting elements of an arrayvoidquicksort (int32_t array[], int32_t length);//The elements of the element group Array[low...high] are sorted so that the elements in front of the pivot are smaller than the pivot elements.//the element behind the pivot is greater than or equal to the pivot element and returns the pivot elementint32_t partition (int32_t array[], int32_t Low, int32_t high); int32_t Main (int32_t argc,Char*argv[]) {int32_t array[]= {3, the,7,3,2, One,9, -, *, -, +}; int32_t length=sizeof(array)/sizeof(array[0]); printf ("before sort:");  Displayarray (array, length);  Quicksort (array, length); printf ("After sort:");  Displayarray (array, length); return 0;}voidDisplayarray (int32_t array[], int32_t length) {int32_t i;  for(i =0; i < length; ++i) printf ("%4d", Array[i]); printf ("\ n");}void_displayarray (int32_t array[], int32_t length, int32_t pivot,  int32_t count) {int32_t i; printf ("The %d times result: [", Count);  for(i =0; I < pivot; ++i) printf ("%-4d", Array[i]); printf ("]"); printf ("%3d", Array[pivot]); printf ("[");  for(i = pivot +1; i < length; ++i) printf ("%-4d", Array[i]); printf ("]"); printf ("\ n");}void_quicksort (int32_t array[], int32_t length, int32_t Low, int32_t high) {int32_t pivot; Staticint32_t count =1; if(Low < High) {//if the length of the element sequence is greater than 1Pivot = partition (array, low, high);//divide the sequence to be sorted Array[low...high] into two parts_displayarray (array, length, pivot, count);//outputs the result of each partition++count; _quicksort (array, length, low, pivot-1);//recursively sorts the left child table, Pivot is the pivot position_quicksort (array, length, Pivot +1, high);//recursively sort the child table on the right  }}voidquicksort (int32_t array[], int32_t length) {_quicksort (array, length,0, Length-1);}  int32_t partition (int32_t array[], int32_t Low, int32_t high) {int32_t temp, pivot; Pivot= Array[low];//use the first element as a pivot elementtemp =Array[low];  while(Low < High) {//alternate to intermediate scan from both ends of the table     while(Low < High && Array[high] >= pivot)//scan forward from the end of the table--High ; if(Low < High) {//saves the current high point element to the low positionArray[low] =Array[high]; ++Low ; }     while(Low < High && Array[low] <= pivot)//backward scanning from the beginning of the table++Low ; if(Low < High) {//saves the element that the current low points to in the high positionArray[high] =Array[low]; --High ; } Array[low]=temp; }  returnLow ;}
Result.

MMORPG large game design and development (multi-threaded server game scene)

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.