вторник, 20 января 2009 г.

Грабли::TerminateThread and memory allocating/deallocating deadlock

Do you remember this piece of code? Looks it similar to you?

void operator delete(
void *pUserData
)
{
_CrtMemBlockHeader * pHead;

RTCCALLBACK(_RTC_Free_hook, (pUserData, 0));

if (pUserData == NULL)
return;

_mlock(_HEAP_LOCK); /* block other threads */ <--- Here it hangs !!!
__TRY
/* get a pointer to memory block header */
pHead = pHdr(pUserData);

/* verify block type*/
_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));

_free_dbg( pUserData, pHead->nBlockUse );

__FINALLY
_munlock(_HEAP_LOCK); /* release other threads */
__END_TRY_FINALLY

return;
}


If you'll kill a thread in the wrong moment after _mlock(_HEAP_LOCK) but before _munlock(_HEAP_LOCK) the next call of a function that uses _HEAP_LOCK will hang!

I've spent a lot of time to find this out...

Some more info:
Improving Performance with Thread-Private Heaps by Kevin Manley

Jochen Kalmbach writes in his blog about this problem also:
Why you should never call Suspend/TerminateThread (Part I)
Why you should never call Suspend/TerminateThread (Part II)

Комментариев нет:

Отправить комментарий