Specific embodiment
Hereinafter, the present invention will be described in detail with reference to the accompanying drawings and in combination with Examples.It should be noted that not conflictingIn the case of, the features in the embodiments and the embodiments of the present application can be combined with each other.
It should be noted that description and claims of this specification and term " first " in above-mentioned attached drawing, "Two " etc. be to be used to distinguish similar objects, without being used to describe a particular order or precedence order.
Embodiments herein provides a kind of processing method of resource, and Fig. 1 is resource according to an embodiment of the present inventionProcessing method flow chart, as shown in Figure 1, the process includes the following steps:
Multiple tasks relevant to texture resource are loaded at least one load thread in load thread pool by step S102Task queue in;
Step S104 obtains pending task from the task queue of at least one load thread, and loads pendingBusiness is to generate image data.
Multiple tasks are loaded at least one of thread pool load thread by S102 and step S104 through the above stepsTask queue in, multiple tasks are recorded simultaneously, is the equal of the asynchronous load of multithreading, increases transient resource and addThe handling capacity of load, but be not that task load is carried out by main thread, vast resources was loaded in a short time to solveIt carries out that game main thread is caused to lead to the problem of Caton phenomenon in Cheng Fangjin game main thread.
It should be noted that the executing subject of the method and step in the embodiment of the present application can be with terminal, server etc..
In addition, being to determine the number for loading thread in load thread pool according to the core number of processor in the embodiment of the present applicationAmount.For example, this is then arranged after having got the core amounts of processor of the equipment for executing the application method and stepThe thread amount of the load thread of thread pool is 2 times of processor core quantity in embodiment.Certainly above-mentioned is only to illustrate, can alsoTo be 1.5 times or 3 times etc., can be arranged accordingly according to the actual situation.
In the optional embodiment of the present embodiment, add for what is be related in the present embodiment step S104 from least oneIt carries in the task queue of thread and obtains pending task, and load pending task in a manner of generating image data, Ke YitongIt crosses such as under type and realizes:
Step S104-11 obtains the identification information of current load thread;
Step S104-12, according to the determining task queue corresponding with current load thread of identification information;
Step S104-13, in the case where the currently corresponding task queue of load thread is non-empty, from current load threadPending task is obtained in corresponding task queue, and loads pending task to generate image data.
S104-11 to step S104-13 through the above steps, for the loading procedure of task, it is necessary first to pass through loadThe determining task queue corresponding with current load thread of the identification information of thread, only just in the case where task queue is non-emptyThe operation of loading tasks can be executed.Certainly, if task queue is sky, load thread is allowed to enter wait state.
In the another embodiment of the present embodiment, obtained from the task queue of at least one load thread in step S104Pending task is taken, and after loading pending task to generate image data, the method for the present embodiment further include:
The image data of generation is input in the consumption queue of main thread by step S106;
It should be noted that the main thread being related in the application is for being responsible for game picture rendering, game logic executesThe thread of equal related works;And thread is loaded for being responsible for the thread of texture resource loading tasks.
Step S108 obtains the image data for being no more than preset quantity from consumption queue to execute texture and generates operation;
It should be noted that the preset quantity being related in above-mentioned steps S106 and step S108 is that the every frame of main thread is maximumThe View data quantity that can be handled.
As it can be seen that only needing to load the image data based on generation for main thread, without to texture resourceMultiple tasks are loaded;Moreover, because the quantity for executing the image data that texture generates operation does not exceed the every frame of main threadThe View data quantity that maximum can be handled, avoids the Caton of main thread.
The application is illustrated below with reference to the alternative embodiment of the present embodiment.
How optimization Cocos2dx game engine is passed through in concrete application scene with the application in this alternative embodimentMode to realize is illustrated.
Firstly, modification engine CCTextureCache module, supports the asynchronous load based on thread pool.
Modification this sector of breakdown data structure as shown in Figure 2, wherein
_ loadingThreads indicates all thread objects in thread pool;
_ asyncStructQueueOnOneThread indicates the loading tasks queue on some current thread;
_ asyncStructQueueMutexOnOneThread indicates the task queue operation mutual exclusion device of some thread;
_ sleepConditionOnOneThread indicates the waiting conditional-variable of some thread;
_ maxCbPerFrame indicates manageable stock number when the every frame of main thread is adjusted back;
_ ioThreadNum indicates the thread amount in current thread pond, can be configured according to actual needs;
_ curThreadIdxToPutTask indicates next task thread to be assigned to;
Then TextureCache::addImageAsync function is modified, as shown in figure 3, in 149~155 rows, when puttingWhen entering a new task, whether the lower thread that will handle this task of judgement has been created, if not having, creates a loadThread executes TextureCache::loadImage function, and creates a task queue for it.In 166 rows, according to parameter,Generate task structure.The task structure that 166 rows generate is put into the task queue of the thread by 169~172 rows.174 rows lead toIt crosses conditional-variable and wakes up the thread.176 rows modify next Thread Id that will receive task.
As it can be seen that by modifying this function, asynchronous operation request will alternately be put into these threads in order and be heldRow.
As shown in figure 4, then modifying TextureCache::loadImage function, in function parameter, we can knowThe ID of road current thread, i.e. threadIdx.By this variable, we can take the task queue of the thread, and take out theOne task carries out IO load.If task queue is sky, the thread is allowed to enter wait state, waited correspondingSleepCondtion conditional-variable issues notify event.It loads and completes to IO, and after generating image data, image dataIt is placed in the consumption queue of main thread, main thread is waited to generate real texture object.
As shown in figure 5, TextureCache::addImageAsyncCallBack function is next modified, due to main lineJourney is limited in the execution time of every frame, it is not desired that main thread processes more image datas in the readjustment of every frame,Cause main thread that Caton occurs.Therefore it joined maxCbPerFrame, control the image data number that every frame maximum can be handled., can be by the way that maxCbPerFrame parameter be arranged such as 335~336 rows, then this function is at most executed from image data queueTexture generation operation is carried out by maxCbPerFrame data are no more than.
As shown in fig. 6, addition TextureCache::setIOThreadNum interface, setting load thread amount.
As shown in fig. 7, addition TextureCache::setMaxCbPerFrame interface, the every frame readjustment of setting main threadAmount.
Secondly, the modification asynchronous loading module of script, is arranged thread amount and readjustment amount, and connect with association's asynchronous load of journey encapsulationMouthful.
As shown in figure 8, in the asynchronous loading module of script, the code in frame above, most provided with the every frame of main threadBig readjustment amount, the code in following frame first obtain the CPU quantity of this equipment, are then provided with 2 times of CPU numbersThe thread amount of amount, for executing asynchronous load operation.
As shown in figure 9, the association Cheng Gongneng provided by Lua language, packaged asynchronous loading interface.Allow to by sameThe written in code asynchronous logic of step form.
Through the above description of the embodiments, those skilled in the art can be understood that according to above-mentioned implementationThe method of example can be realized by means of software and necessary general hardware platform, naturally it is also possible to by hardware, but it is very muchIn the case of the former be more preferably embodiment.Based on this understanding, technical solution of the present invention is substantially in other words to existingThe part that technology contributes can be embodied in the form of software products, which is stored in a storageIn medium (such as ROM/RAM, magnetic disk, CD), including some instructions are used so that a terminal device (can be mobile phone, calculateMachine, server or network equipment etc.) execute method described in each embodiment of the present invention.
A kind of processing unit of resource is additionally provided in embodiments herein, the device is for realizing above-described embodimentAnd preferred embodiment, the descriptions that have already been made will not be repeated.As used below, term " module " may be implemented to make a reservation forThe combination of the software and/or hardware of function.It is hard although device described in following embodiment is preferably realized with softwareThe realization of the combination of part or software and hardware is also that may and be contemplated.
Figure 10 is the structural block diagram of the processing unit of resource according to an embodiment of the present invention, as shown in Figure 10, the device packetIt includes: loading module 22, for multiple tasks relevant to texture resource to be loaded at least one loaded line in load thread poolIn the task queue of journey;First processing module 24 is of coupled connections with loading module 22, for appointing from least one load threadPending task is obtained in business queue, and loads pending task to generate image data.
Optionally, first processing module includes: acquiring unit, for obtaining the identification information of current load thread;It determinesUnit, for according to the determining task queue corresponding with current load thread of identification information;Processing unit, for being loaded currentlyIn the case that the corresponding task queue of thread is non-empty, pending is obtained from the corresponding task queue of current load threadBusiness, and pending task is loaded to generate image data.
Figure 11 is the alternative construction block diagram of the processing unit of resource according to an embodiment of the present invention, as shown in figure 11, the dressIt sets further include: input module 32 is of coupled connections with first processing module 24, in the task team from least one load threadPending task is obtained in column, and after loading pending task to generate image data, the image data of generation is input toIn the consumption queue of main thread;Second processing module 34 is of coupled connections with input module 32, for obtaining not from consumption queueOperation is generated more than the image data of preset quantity to execute texture.
It should be noted that above-mentioned modules can be realized by software or hardware, for the latter, Ke YitongFollowing manner realization is crossed, but not limited to this: above-mentioned module is respectively positioned in same processor;Alternatively, above-mentioned modules are with anyCombined form is located in different processors.
Embodiments herein additionally provides a kind of storage medium, is stored with computer program in the storage medium, whereinThe computer program is arranged to execute the step in any of the above-described embodiment of the method when operation.
Optionally, in the present embodiment, above-mentioned storage medium can be set to store by executing based on following stepsCalculation machine program:
Multiple tasks relevant to texture resource are loaded into the task of at least one load thread in load thread pool by S1In queue;
S2 obtains pending task from the task queue of at least one load thread, and loads pending task with lifeAt image data.
Optionally, in the present embodiment, above-mentioned storage medium can include but is not limited to: USB flash disk, read-only memory (Read-Only Memory, referred to as ROM), it is random access memory (Random Access Memory, referred to as RAM), mobile hardThe various media that can store computer program such as disk, magnetic or disk.
Embodiments herein additionally provides a kind of electronic device, including memory and processor, stores in the memoryThere is computer program, which is arranged to run computer program to execute the step in any of the above-described embodiment of the methodSuddenly.
Optionally, above-mentioned electronic device can also include transmission device and input-output equipment, wherein the transmission deviceIt is connected with above-mentioned processor, which connects with above-mentioned processor.
Optionally, in the present embodiment, above-mentioned processor can be set to execute following steps by computer program:
Multiple tasks relevant to texture resource are loaded into the task of at least one load thread in load thread pool by S1In queue;
S2 obtains pending task from the task queue of at least one load thread, and loads pending task with lifeAt image data.
Optionally, the specific example in the present embodiment can be with reference to described in above-described embodiment and optional embodimentExample, details are not described herein for the present embodiment.
Obviously, those skilled in the art should be understood that each module of the above invention or each step can be with generalComputing device realize that they can be concentrated on a single computing device, or be distributed in multiple computing devices and formedNetwork on, optionally, they can be realized with the program code that computing device can perform, it is thus possible to which they are storedIt is performed by computing device in the storage device, and in some cases, it can be to be different from shown in sequence execution hereinOut or description the step of, perhaps they are fabricated to each integrated circuit modules or by them multiple modules orStep is fabricated to single integrated circuit module to realize.In this way, the present invention is not limited to any specific hardware and softwares to combine.
The foregoing is only a preferred embodiment of the present invention, is not intended to restrict the invention, for the skill of this fieldFor art personnel, the invention may be variously modified and varied.It is all within principle of the invention, it is made it is any modification, etc.With replacement, improvement etc., should all be included in the protection scope of the present invention.