Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Archived:Memory allocation in multithreaded Symbian C++ applications

From Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

Article Metadata
Platform(s): S60 1st Edition
S60 2nd Edition, FP1, FP2, FP2
S60 3rd Edition
S60 3rd Edition FP1
S60 3rd Edition FP1
S60 3rd Edition (initial release)
S60 2nd Edition (initial release)
S60 1st Edition
Created: User:Technical writer 2 (04 Jul 2007)
Last edited: hamishwillee (14 Jun 2012)


Calls like User::Alloc() and User::Available() are not thread safe if used in multiple threads with a shared heap.


User::Alloc() and User::Available() calls are not thread safe by themselves. In a multithreaded application it is possible for the parent thread to specify the heap used by a child thread. There are two options: dedicated heap and shared heap. If two or more threads operate on a shared heap, then synchronization (e.g. with mutexes) is required.


1) Shared heap:
  IMPORT_C TInt Create( const TDesC &aName,
                        TThreadFunction aFunction,
                        TInt aStackSize,
                        RAllocator *aHeap,
                        TAny *aPtr,
                        TOwnerType aType = EOwnerProcess );                   
If the aHeap parameter is set as NULL in the above call, then the thread will be sharing a heap memory with the parent thread.
In this case, memory allocation should be synchronized in their respective threads:
  RMutex sharedMutex;

  array[i] = User::Alloc( bytesToAlloc );
  TInt heapSize = User::Available( largestBlock );
2) Dedicated heap:
When the following overloaded version of RThread::Create() is used, the new thread will use its own independent heap.
  IMPORT_C TInt Create( const TDesC &aName,
                        TThreadFunction aFunction,
                        TInt aStackSize,
                        TInt aHeapMinSize,
                        TInt aHeapMaxSize,
                        TAny *aPtr,
                        TOwnerType aType = EOwnerProcess );
With separate heaps, the memory allocation calls do not have to be synchronized.

This page was last modified on 14 June 2012, at 07:50.
42 page views in the last 30 days.