Pthread instance, pthread instance
In the cocos2d-x with multithreading or good with pthread. When application 1 is started, a thread is created as a logical thread.
The creation method is as follows:
Extern ResourcePool logicThreadResource; void * test (void * param) {while (true) {if (logicThreadResource. peekNextDataLength () {printf ("data to be processed"); int datasize = 0; char * receiveData = new char [512]; logicThreadResource. popData (receiveData, datasize, 512); int sigal = * (int32_t *) receiveData); printf ("the sigal is % d \ n", sigal ); if (sigal = 100) {CalWinRate (receiveData + 4);} delete [] receiveData;} else {pthread_mutex_lock (& locklogicthread); pthread_cond_wait (& cond, & locklogicthread ); pthread_mutex_unlock (& locklogicthread) ;}return NULL ;}void create_pthread () {// start the main threads pthread_t tid for two tests; pthread_create (& tid, NULL, test, NULL );}Pthread_create: PTW32_DLLPORT int PTW32_CDECL pthread_create (pthread_t * tid, const pthread_attr_t * attr, void * (* start) (void *), void * arg ). Create has four parameters: the first is pthread_t, the second is the thread creation parameter, the third is the thread entry function, and the fourth is the entry function parameter. Note that when pthread_cond_wait is set, the lock must be applied to the wake-up function. The startup function is as follows:
Bool ResourcePool: pushData (const char * data, int size) {AutoReleaseLock myLock = AutoReleaseLock (); // deal int realSize = size + sizeof (int32_t ); int needMemory = bufferLength + realSize; if (needMemory> bufferCapacity) {// memory needs to be re-allocated if (remolocmemory (needMemory) {} else {return false ;}} // char * pWriteBuff = resPool + bufferLength; * (int32_t *) pWriteBuff) = size; pWriteBuff + = sizeof (int32_t); bufferLength + = sizeof (int32_t ); memmove (pWriteBuff, data, size); bufferLength + = size; // activate the thread pthread_cond_signal (& cond); return true if the buffer has data ;}
The idea is still the use of my blog ideas, as for the call inside the cocos2d-x I am like this:
Local handlerlocal schedctor = CCDirector: shareddire(): getschedctor () handler = schedctor: scheduleScriptFunc (function ()-check data every frame to see if it is reasonable BookUtils: parsePackageToLua () if GLOABLEPACKAGE ~ = "" Then -- parse data local package = json. decode (GLOABLEPACKAGE) print ("the GLOABLEPACKAGE is ".. GLOABLEPACKAGE) GLOABLEPACKAGE = "" local able = {} table. walk (package, function (val, key) if tonumber (key )~ = 1 then dataTable [# dataTable + 1] = val end) if tonumber (package [1]) = 100 then -- victory rate calculator, update victory rate CCNotificationCenter: sharedNotificationCenter (): postNotification ("CAL_WIN_RATE_UPDATE", CCString: create (json. encode (dataTable) elseif tonumber (package [1]) = 101 then -- victory rate calculated CCNotificationCenter: sharedNotificationCenter (): postNotification ("CAL_WIN_RATE_UPDATE_END") end, 0, false)
That is, the buffer zone is detected for each frame of the main thread. If there is data, a message is sent.