Pthread instance, pthread instance

Source: Internet
Author: User

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.

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.