Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Improving Performance with Thread-Private Heaps


September 1999/Improving Performance with Thread-Private Heaps/Listing 1

Listing 1: A program to measure multithreaded performance using a single process heap vs. thread-private heaps

// main.cpp
#include <windows.h>
#include <process.h>
#include <iostream>
#include <assert.h>

using namespace std; 

int nNumThreads = 0; 
int nNumAllocs = 0; 
int nAllocSize = 0; 
HANDLE hStartEvent = NULL; 

unsigned __stdcall threadfunc1( void* pParm )
{
   HANDLE hHeap = (HANDLE) pParm; 
   for( int i=0; i<nNumAllocs; i++ ) {
      char* p = (char*) HeapAlloc( hHeap, 0, nAllocSize );
      HeapFree( hHeap, 0, p ); 
   } // for
   return 0; 
}


unsigned __stdcall threadfunc2( void* pParm )
{
   HANDLE hHeap = (HANDLE) pParm; 
   for( int i=0; i<nNumAllocs; i++ ) {
      char* p = 
         (char*) HeapAlloc(hHeap, HEAP_NO_SERIALIZE, nAllocSize);
      HeapFree( hHeap, HEAP_NO_SERIALIZE, p ); 
   } // for
   return 0; 
}


void 
go( char* pTest, char* pNumThreads, char* pNumAllocs, 
   char* pAllocSize )
{
   nNumThreads = atoi( pNumThreads ); 
   nNumAllocs = atoi( pNumAllocs );
   nAllocSize = atoi( pAllocSize ); 

   DWORD dwStart, dwEnd; 
   BOOL bOneHeap = ( lstrcmp( pTest, "1") == 0 ); 
   if( bOneHeap ) {
      cout << "one heap per process" << endl; 
   } else {
      cout << "one heap per thread" << endl; 
   } // else
   cout << nNumThreads << " threads" << endl; 
   cout << nNumAllocs << " allocations per thread" << endl; 
   cout << nAllocSize << " bytes per allocation" << endl; 

   HANDLE* aThreads = new HANDLE[nNumThreads]; 
   HANDLE* aHeaps = NULL; 
   if( !bOneHeap ) {
      // Create a heap for each thread
      aHeaps = new HANDLE[nNumThreads]; 
      for( int i=0; i<nNumThreads; i++ ) {
         aHeaps[i] = HeapCreate(HEAP_NO_SERIALIZE, 0x100000, 0);
      } // for
   } // if

   unsigned int notused; 
   for( int i=0; i<nNumThreads; i++ ) {
      aThreads[i] = 
         (HANDLE) _beginthreadex(NULL, 0, 
                     (bOneHeap ? threadfunc1 : threadfunc2),
                     (bOneHeap ? GetProcessHeap() : aHeaps[i]), 
                     CREATE_SUSPENDED, ¬used ); 
   } // for

   dwStart = GetTickCount();
   for( i=0; i<nNumThreads; i++ ) {
      DWORD dwResult = ResumeThread( aThreads[i] ); 
      assert( dwResult != 0xffffffff );
   } // for

   WaitForMultipleObjects(nNumThreads, aThreads, TRUE, INFINITE);
   dwEnd = GetTickCount(); 

   delete [] aThreads; 
   if( !bOneHeap ) {
      for( i=0; i<nNumThreads; i++ ) {
         HeapDestroy( aHeaps[i] ) ;
      } // for
      delete [] aHeaps;
   } // if

   cout << "time: " << (dwEnd-dwStart) << endl; 
}


int main(int argc, TCHAR* argv[], TCHAR* envp[])
{
   if( argc != 5 ) {
      cout << "usage: HeapDemo <1|2> <numthreads>"
           << " <numallocs> <allocsize>" << endl;
      cout << "first param=1 uses one process wide heap "
           << "(heap access is serialized)" << endl; 
      cout << "first param=2 uses one heap per thread "
           << "(no heap serialization)" << endl; 
      return 0; 
   } else {
      go( argv[1], argv[2], argv[3], argv[4] ); 
   } // else
   return 0; 
}


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.