|
1 <?xml version="1.0" encoding="utf-8"?> |
|
2 <!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. --> |
|
3 <!-- This component and the accompanying materials are made available under the terms of the License |
|
4 "Eclipse Public License v1.0" which accompanies this distribution, |
|
5 and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". --> |
|
6 <!-- Initial Contributors: |
|
7 Nokia Corporation - initial contribution. |
|
8 Contributors: |
|
9 --> |
|
10 <!DOCTYPE concept |
|
11 PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd"> |
|
12 <concept id="GUID-CFA21FBA-593F-58DB-AADA-C9D6D676AEEE" xml:lang="en"><title>How |
|
13 to share heaps</title><shortdesc>Heaps may be shared between threads within a process.</shortdesc><prolog><metadata><keywords/></metadata></prolog><conbody> |
|
14 <p>When a new thread is created:</p> |
|
15 <ul> |
|
16 <li id="GUID-7FF23B75-DADB-557C-B8F7-C1884E51FCE4"><p>it can use the same |
|
17 heap as the thread which is doing the creating.</p> </li> |
|
18 <li id="GUID-19F6D491-9424-5320-BFF7-DFB27008D90B"><p>it can use the heap |
|
19 which has been explicitly created for it by the thread which is doing the |
|
20 creating.</p> </li> |
|
21 <li id="GUID-83CB8A55-DB7C-503C-8CB8-E206C918FF99"><p>it can use the heap |
|
22 automatically created for it by the operating system.</p> </li> |
|
23 </ul> |
|
24 <p><i>Only in the first two cases is the heap being shared.</i> </p> |
|
25 <p>Both are achieved by using the version of <codeph>RThread::Create()</codeph> prototyped |
|
26 as:</p> |
|
27 <codeblock id="GUID-7716A80C-634E-55A2-BB22-EEB621DFFA40" xml:space="preserve">TInt Create(const TDesC& aName, |
|
28 TThreadFunction aFunction, |
|
29 TInt aStackSize, |
|
30 RHeap* aHeap, |
|
31 TAny* aPtr, |
|
32 TOwnerType aType=EOwnerProcess);</codeblock> |
|
33 <p>If <codeph>aHeap</codeph> is <codeph>NULL</codeph>, the new thread uses |
|
34 the same heap as the parent thread. For example:</p> |
|
35 <codeblock id="GUID-0F13C320-55FD-5570-89EF-109023AD4F0A" xml:space="preserve">RThread t; |
|
36 _LIT(KTxtShared1,"Shared1"); |
|
37 ... ... |
|
38 TInt r=t.Create(KTxtShared1, |
|
39 threadEntryPoint, |
|
40 KDefaultStackSize, |
|
41 NULL, |
|
42 NULL);</codeblock> |
|
43 <p>The calling thread can create a new heap using <codeph>User::ChunkHeap()</codeph> or <codeph>UserHeap::ChunkHeap()</codeph> (this |
|
44 is the same function; the <codeph>User</codeph> class is derived from <codeph>UserHeap</codeph>) |
|
45 and pass this heap to the <codeph>RThread::Create()</codeph> function. For |
|
46 example:</p> |
|
47 <codeblock id="GUID-9DC2E0EF-6606-54FC-A2DE-3987620784A9" xml:space="preserve">_LIT(KTxtShare,"Share"); |
|
48 _LIT(KTxtShared1,"Shared1"); |
|
49 ... |
|
50 RHeap* pH=User::ChunkHeap(KTxtShare,0x1000,0x100000); |
|
51 RThread t; |
|
52 TInt r=t.Create(KTxtShared1, |
|
53 threadEntryPoint, |
|
54 KDefaultStackSize, |
|
55 pH,NULL);</codeblock> |
|
56 <p>The new heap is contained within its own chunk.</p> |
|
57 <p>If the <i>creating</i> thread no longer has any interest in this explicitly |
|
58 created heap, it can close it by calling <codeph>Close()</codeph>. Note that |
|
59 the corresponding call to <codeph>Open()</codeph> was done automatically by <codeph>User::ChunkHeap()</codeph>. |
|
60 The function <codeph>Open()</codeph> should be used to re-open the heap for |
|
61 sharing; this increases the access count.</p> |
|
62 <p>Note that <codeph>Open()</codeph> can only be called on a heap created |
|
63 using <codeph>User::ChunkHeap()</codeph>.</p> |
|
64 <p>The heaps which are created automatically for a thread should not be closed |
|
65 with a call to <codeph>Close()</codeph> since the chunk in which the heap |
|
66 is created contains the thread's stack as well as the heap. In this case, |
|
67 if <codeph>Close()</codeph> is called the thread is panicked.</p> |
|
68 </conbody></concept> |