Cocos2dx multithreading and thread synchronization and cocos2dx memory management and Multithreading

Source: Internet
Author: User
-------------------------------------------------------------------- CCPoolManager -------------------------------------------------------------------- [diff

// Notify // CCPoolManager /// notify // [diff-begin]-by layne // CCPoolManager * CCPoolManager: shared

//--------------------------------------------------------------------

//

// CCPoolManager

//

//--------------------------------------------------------------------


//// [Diff-begin]-by layne //////


CCPoolManager * CCPoolManager: sharedPoolManager ()

{

If (s_pPoolManager = NULL)

{

S_pPoolManager = new CCPoolManager ();

}

Return s_pPoolManager;

}


Void CCPoolManager: purgePoolManager ()

{

CC_SAFE_DELETE (s_pPoolManager );

}


CCPoolManager: CCPoolManager ()

{

// M_pReleasePoolStack = new CCArray ();

// M_pReleasePoolStack-> init ();

// M_pCurReleasePool = 0;


M_pReleasePoolMultiStack = new CCDictionary ();

}


CCPoolManager ::~ CCPoolManager ()

{


// Finalize ();


/// We only release the last autorelease pool here

// M_pCurReleasePool = 0;

// M_pReleasePoolStack-> removeObjectAtIndex (0 );

//

// CC_SAFE_DELETE (m_pReleasePoolStack );


Finalize ();


CC_SAFE_DELETE (m_pReleasePoolMultiStack );

}


Void CCPoolManager: finalize ()

{

If (m_pReleasePoolMultiStack-> count ()> 0)

{

// CCAutoreleasePool * pReleasePool;

CCObject * pkey = NULL;

CCARRAY_FOREACH (m_pReleasePoolMultiStack-> allKeys (), pkey)

{

If (! Pkey)

Break;

CCInteger * key = (CCInteger *) pkey;

CCArray * poolStack = (CCArray *) m_pReleasePoolMultiStack-> objectForKey (key-> getValue ());

CCObject * pObj = NULL;

CCARRAY_FOREACH (poolStack, pObj)

{

If (! PObj)

Break;

CCAutoreleasePool * pPool = (CCAutoreleasePool *) pObj;

PPool-> clear ();

}

}

}

}


Void CCPoolManager: push ()

{

// CCAutoreleasePool * pPool = new CCAutoreleasePool (); // ref = 1

// M_pCurReleasePool = pPool;

//

// M_pReleasePoolStack-> addObject (pPool); // ref = 2

//

// PPool-> release (); // ref = 1


Pthread_mutex_lock (& m_mutex );


CCArray * pCurReleasePoolStack = getCurReleasePoolStack ();

CCAutoreleasePool * pPool = new CCAutoreleasePool (); // ref = 1

PCurReleasePoolStack-> addObject (pPool); // ref = 2

PPool-> release (); // ref = 1


Pthread_mutex_unlock (& m_mutex );

}


Void CCPoolManager: pop ()

{

// If (! M_pCurReleasePool)

//{

// Return;

//}

//

// Int nCount = m_pReleasePoolStack-> count ();

//

// M_pCurReleasePool-> clear ();

//

// If (nCount> 1)

//{

// M_pReleasePoolStack-> removeObjectAtIndex (nCount-1 );

//

/// If (nCount> 1)

////{

/// M_pCurReleasePool = m_pReleasePoolStack-> objectAtIndex (nCount-2 );

//// Return;

////}

// M_pCurReleasePool = (CCAutoreleasePool *) m_pReleasePoolStack-> objectAtIndex (nCount-2 );

//}

//

/// * M_pCurReleasePool = NULL ;*/


Pthread_mutex_lock (& m_mutex );


CCArray * pCurReleasePoolStack = getCurReleasePoolStack ();

CCAutoreleasePool * pCurReleasePool = getCurReleasePool ();

If (pCurReleasePoolStack & pCurReleasePool)

{

Int nCount = pCurReleasePoolStack-> count ();


PCurReleasePool-> clear ();


If (nCount> 1)

{

PCurReleasePoolStack-> removeObject (pCurReleasePool );

}

}


Pthread_mutex_unlock (& m_mutex );

}


Void CCPoolManager: removeObject (CCObject * pObject)

{

// CCAssert (m_pCurReleasePool, "current auto release pool shocould not be null ");

//

// M_pCurReleasePool-> removeObject (pObject );


Pthread_mutex_lock (& m_mutex );

CCAutoreleasePool * pCurReleasePool = getCurReleasePool ();

CCAssert (pCurReleasePool, "current auto release pool shocould not be null ");


PCurReleasePool-> removeObject (pObject );

Pthread_mutex_unlock (& m_mutex );

}


Void CCPoolManager: addObject (CCObject * pObject)

{

// GetCurReleasePool ()-> addObject (pObject );


Pthread_mutex_lock (& m_mutex );

CCAutoreleasePool * pCurReleasePool = getCurReleasePool (true );

CCAssert (pCurReleasePool, "current auto release pool shocould not be null ");


PCurReleasePool-> addObject (pObject );

Pthread_mutex_unlock (& m_mutex );

}


CCArray * CCPoolManager: getCurReleasePoolStack ()

{

CCArray * pPoolStack = NULL;

Pthread_t tid = pthread_self ();

If (m_pReleasePoolMultiStack-> count ()> 0)

{

PPoolStack = (CCArray *) m_pReleasePoolMultiStack-> objectForKey (int) tid );

}


If (! PPoolStack ){

PPoolStack = new CCArray ();

M_pReleasePoolMultiStack-> setObject (pPoolStack, (int) tid );

PPoolStack-> release ();

}


Return pPoolStack;

}


CCAutoreleasePool * CCPoolManager: getCurReleasePool (bool autoCreate)

{

// If (! M_pCurReleasePool)

//{

// Push ();

//}

//

// CCAssert (m_pCurReleasePool, "current auto release pool shocould not be null ");

//

// Return m_pCurReleasePool;


CCAutoreleasePool * pReleasePool = NULL;



CCArray * pPoolStack = getCurReleasePoolStack ();

If (pPoolStack-> count ()> 0)

{

PReleasePool = (CCAutoreleasePool *) pPoolStack-> lastObject ();

}


If (! PReleasePool & autoCreate ){

CCAutoreleasePool * pPool = new CCAutoreleasePool (); // ref = 1

PPoolStack-> addObject (pPool); // ref = 2

PPool-> release (); // ref = 1


PReleasePool = pPool;

}


Return pReleasePool;

}


//// [Diff-end]-by layne //////



Code: https://github.com/kaitiren/pthread-test-for-cocos2dx

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.