Symbian3/SDK/Source/GUID-056B9A65-EC59-4B2E-9F10-E487343B5F5F.dita
changeset 13 48780e181b38
parent 7 51a74ef9ed63
equal deleted inserted replaced
12:80ef3a206772 13:48780e181b38
     7     Nokia Corporation - initial contribution.
     7     Nokia Corporation - initial contribution.
     8 Contributors: 
     8 Contributors: 
     9 -->
     9 -->
    10 <!DOCTYPE concept
    10 <!DOCTYPE concept
    11   PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
    11   PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
    12 <concept id="GUID-056B9A65-EC59-4B2E-9F10-E487343B5F5F" xml:lang="en"><title>SemaphoreExample:
    12 <concept id="GUID-056B9A65-EC59-4B2E-9F10-E487343B5F5F" xml:lang="en"><title>SemaphoreExample: thread synchronisation using semaphores</title><shortdesc>Explains how to use Semaphores to achieve thread synchronization.</shortdesc><prolog><metadata><keywords/></metadata></prolog><conbody>
    13 thread synchronisation using semaphores</title><shortdesc>Explains how to use Semaphores to achieve thread synchronization.</shortdesc><prolog><metadata><keywords/></metadata></prolog><conbody>
    13 <p>It is recommended to use semaphores instead of thread priorities
    14 <p>It is recommended to use semaphores instead of thread priorities for the
    14 for the following reasons:<ul>
    15 following reasons:<ul>
    15 <li><p> In a uniprocessor environment, setting the thread priorities
    16 <li><p> In a uniprocessor environment, setting the thread priorities does
    16 does not guarantee prioritized or serialized execution.</p></li>
    17 not guarantee prioritized or serialized execution.</p></li>
    17 <li><p>In a multiprocessor or SMP environment, thread priorities are
    18 <li><p>In a multiprocessor or SMP environment, thread priorities are irrelevant
    18 irrelevant as multiple threads can run simultaneously.</p></li>
    19 as multiple threads can run simultaneously.</p></li>
       
    20 </ul></p>
    19 </ul></p>
    21 <section id="GUID-48668C9B-6B42-4FE8-899B-403D5EB5FB53"><title>Download</title> <p>Click on the following link to download
    20 <section id="GUID-48668C9B-6B42-4FE8-899B-403D5EB5FB53"><title>Download</title> <p>Click on the following link to download the example: <xref href="guid-6013a680-57f9-415b-8851-c4fa63356636/zips/guid-34775913-c5c2-41ba-a591-827039bbe7af.zip" scope="external">SemaphoreExample.zip</xref></p><p>Click: <xref href="guid-6013a680-57f9-415b-8851-c4fa63356636/guid-34775913-c5c2-41ba-a591-827039bbe7af.html" scope="peer">browse</xref> to view the example code. </p></section>
    22 the example: <xref href="guid-6013a680-57f9-415b-8851-c4fa63356636/zips/guid-34775913-c5c2-41ba-a591-827039bbe7af.zip" scope="external">SemaphoreExample.zip</xref></p><p>Click: <xref href="guid-6013a680-57f9-415b-8851-c4fa63356636/guid-34775913-c5c2-41ba-a591-827039bbe7af.html" scope="peer">browse</xref> to view the example code. </p></section>
    21 <section id="GUID-2E531F3D-EEC8-40FD-9F32-2071FDECAE61"><title>Description: </title><p> The example creates a database containing a table of three columns
    23 <section id="GUID-2E531F3D-EEC8-40FD-9F32-2071FDECAE61"><title>Description: </title><p> The example creates a database containing
    22 with integer entries.</p><p> The example creates two threads:</p><ul>
    24 a table of three columns with integer entries.</p><p> The example creates
    23 <li><p><codeph>WriterThread</codeph>: This thread calls a periodic
    25 two threads:</p><ul>
    24 function at an interval of one second, and in each iteration it inserts
    26 <li><p><codeph>WriterThread</codeph>: This thread calls a periodic function
    25 one row of integers in the table. </p></li>
    27 at an interval of one second, and in each iteration it inserts one row of
    26 <li><p><codeph>ReaderThread</codeph>: This thread also calls a periodic
    28 integers in the table. </p></li>
    27 function at an interval of one second, and in each iteration it reads
    29 <li><p><codeph>ReaderThread</codeph>: This thread also calls a periodic function
    28 one row of integers following the last row read.</p></li>
    30 at an interval of one second, and in each iteration it reads one row of integers
    29 </ul><p> The example uses a macro <codeph>USE_SEMAPHORE</codeph> to
    31 following the last row read.</p></li>
    30 show the difference between the code using semaphores and the code
    32 </ul><p> The example uses a macro <codeph>USE_SEMAPHORE</codeph> to show the
    31 which doesn't use semaphores. When not using semaphores, it is not
    33 difference between the code using semaphores and the code which doesn't use
    32 guaranteed that the <codeph>ReaderThread</codeph> will be able to
    34 semaphores. When not using semaphores, it is not guaranteed that the <codeph>ReaderThread</codeph> will
    33 read the database because in an SMP environment, data may not yet
    35 be able to read the database because in an SMP environment, data may not yet
    34 have been written by the <codeph>WriterThread</codeph>.</p><p>Using
    36 have been written by the <codeph>WriterThread</codeph>.</p><p>Using a semaphore
    35 a semaphore serializes the thread execution. The read operation waits
    37 serializes the thread execution. The read operation waits on the semaphore
    36 on the semaphore until it gets a signal from the write operation.
    38 until it gets a signal from the write operation. This ensures that the <codeph>ReaderThread</codeph> only
    37 This ensures that the <codeph>ReaderThread</codeph> only reads a row
    39 reads a row from the database after the <codeph>WriterThread</codeph> has
    38 from the database after the <codeph>WriterThread</codeph> has written
    40 written it. </p></section>
    39 it. </p></section>
    41 <section id="GUID-07CAD3CA-86DB-4245-8565-C618E59718A5"><title>Class summary</title><ul>
    40 <section id="GUID-07CAD3CA-86DB-4245-8565-C618E59718A5"><title>Class
       
    41 summary</title><ul>
    42 <li><p><xref href="GUID-B0E661BC-4058-3256-B9C3-5A4FD52F6DE5.dita"><apiname>RThread</apiname></xref> - A handle to a thread.</p></li>
    42 <li><p><xref href="GUID-B0E661BC-4058-3256-B9C3-5A4FD52F6DE5.dita"><apiname>RThread</apiname></xref> - A handle to a thread.</p></li>
    43 <li><p><xref href="GUID-F4A68645-2265-307F-B951-8295827E000B.dita"><apiname>RDbStoreDatabase</apiname></xref> - DBMS Store database implementation</p></li>
    43 <li><p><xref href="GUID-F4A68645-2265-307F-B951-8295827E000B.dita"><apiname>RDbStoreDatabase</apiname></xref> - DBMS Store database implementation</p></li>
    44 <li><p><xref href="GUID-AED27A76-3645-3A04-B80D-10473D9C5A27.dita"><apiname>RSemaphore</apiname></xref> - Used for thread synchronization. </p></li>
    44 <li><p><xref href="GUID-AED27A76-3645-3A04-B80D-10473D9C5A27.dita"><apiname>RSemaphore</apiname></xref> - Used for thread synchronization. </p></li>
    45 </ul></section>
    45 </ul></section>
    46 <section id="GUID-863B0EDF-F3F8-4345-9FEE-824350A2DBCE"><title>Building and configuring</title> <p>To build the example: </p> <ul>
    46 <section id="GUID-863B0EDF-F3F8-4345-9FEE-824350A2DBCE"><title>Building
    47 <li id="GUID-037442C7-2436-5152-9A3E-9DC0B4283A9C"><p>The example builds an
    47 and configuring</title> <p>To build the example: </p> <ul>
    48 executable called <filepath>semaphoreexample.exe</filepath> in the standard
    48 <li id="GUID-037442C7-2436-5152-9A3E-9DC0B4283A9C"><p>The example
    49 location. </p> </li>
    49 builds an executable called <filepath>semaphoreexample.exe</filepath> in the standard location. </p> </li>
    50 <li id="GUID-B7C849B9-0885-5253-A78B-71854F86D772"><p>You can build the example
    50 <li id="GUID-B7C849B9-0885-5253-A78B-71854F86D772"><p>You can build
    51 from your IDE or the command line. </p> <p>If you use an IDE, import the <filepath>bld.inf</filepath> file
    51 the example from your IDE or the command line. </p> <p>If you use
    52 of the example into your IDE, and use the build command of the IDE. </p> <p>If
    52 an IDE, import the <filepath>bld.inf</filepath> file of the example
    53 you use the command line, open a command prompt, and set the current directory
    53 into your IDE, and use the build command of the IDE. </p> <p>If you
    54 to the source code directory of the example. You can then build the example
    54 use the command line, open a command prompt, and set the current directory
    55 with the SBSv1 build tools with the following commands: </p> <p><userinput>bldmake
    55 to the source code directory of the example. You can then build the
    56 bldfiles</userinput> </p> <p><userinput>abld build</userinput> </p> <p><xref href="GUID-793A5EF9-CC16-5EEB-9011-6431EA76EB15.dita">How to use bldmake</xref> and <xref href="GUID-B6B54E07-3B34-5D5C-8815-93383FA8FB4B.dita">How to use abld</xref> describe
    56 example with the SBSv1 build tools with the following commands: </p> <p><userinput>bldmake bldfiles</userinput> </p> <p><userinput>abld
    57 how to use the SBSv1 build tools. </p> </li>
    57 build</userinput> .</p> </li>
    58 </ul> </section>
    58 </ul> </section>
    59 <section id="GUID-278DD8CF-28AF-4826-B938-20EC19F3A5B0"><title>Running the example</title> <p>The example code contains a
    59 <section id="GUID-278DD8CF-28AF-4826-B938-20EC19F3A5B0"><title>Running
    60 macro<codeph>USE_SEMAPHORE</codeph> in the <filepath>mmp</filepath> file.
    60 the example</title> <p>The example code contains a macro<codeph>USE_SEMAPHORE</codeph> in the <filepath>mmp</filepath> file. By default it is commented
    61 By default it is commented out and on execution the example runs without using
    61 out and on execution the example runs without using semaphores; the
    62 semaphores; the macro should be uncommented to use semaphores. The console
    62 macro should be uncommented to use semaphores. The console output
    63 output will generally look the same whether using semaphores or not if there
    63 will generally look the same whether using semaphores or not if there
    64 is a single processor, but differences will occur in an SMP environment. Run
    64 is a single processor, but differences will occur in an SMP environment.
    65 the example following the instructions provided in the console. </p> </section>
    65 Run the example following the instructions provided in the console. </p> </section>
    66 </conbody></concept>
    66 </conbody></concept>