Symbian3/PDK/Source/GUID-B4039418-6499-555D-AC24-9B49161299F2.dita
changeset 5 f345bda72bc4
parent 3 46218c8b8afa
child 14 578be2adaf3e
equal deleted inserted replaced
4:4816d766a08a 5:f345bda72bc4
     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 xml:lang="en" id="GUID-B4039418-6499-555D-AC24-9B49161299F2"><title>Creating a Periodic Timer</title><prolog><metadata><keywords/></metadata></prolog><conbody><p>You can use a periodic timer to provide repeated signals to your process. </p> <p>The following example illustrates a periodic timer with a delay of a second and a repeating interval of ten milliseconds. It also configures a thread function as the timer expiry notification using <codeph>SIGEV_THREAD</codeph>. </p> <p>The following example code performs the following tasks: </p> <ol id="GUID-B648631F-8CC2-5304-B2DC-6A9293385E3E"><li id="GUID-228938F5-3F52-5853-96FC-2556650A617A"><p>Creates a notification function (thread function) that must be invoked after timer expiry. </p> </li> <li id="GUID-7F02B8B1-7AE3-566A-A864-A64B45837C79"><p>Sets the thread priority to <b>255</b> using the thread scheduling parameters (<codeph>struct sched_param</codeph>). This ensures that the thread function has the highest priority when it is invoked as a result of a timer expiry. </p> </li> <li id="GUID-14F0D0AC-65EF-56B6-B82A-EC41C4DDA94F"><p>Creates a timer based on the current system time (<codeph>CLOCK_REALTIME</codeph>) and a notification function (<codeph>struct
    12 <concept id="GUID-B4039418-6499-555D-AC24-9B49161299F2" xml:lang="en"><title>Creating
    13              sigevent sig</codeph>) that must be invoked when the timer expires. </p> </li> <li id="GUID-0A915D17-5113-563B-9CE4-7A7B7B7D7B23"><p>Defines the input values for <xref href="GUID-344F836B-FAC8-344A-B458-0CEFB714ADEB.dita"><apiname>timer_settime()</apiname></xref>. The key input values are the timer value (<codeph>in.it_value.tv_sec = 1;</codeph>) and the interval (<codeph>in.it_interval.tv_nsec = 100000000;</codeph>). The periodic timer will expire after a second and then invoke the notification function every one-tenth of a second until it is destroyed. </p> </li> <li id="GUID-126A5B20-133D-585A-9FF9-9CB5456D2D71"><p>Starts the periodic timer using <xref href="GUID-344F836B-FAC8-344A-B458-0CEFB714ADEB.dita"><apiname>timer_settime()</apiname></xref>. </p> </li> <li id="GUID-FD1EEA8C-3215-5DBE-BB29-47576DED3955"><p>Uses <codeph>sleep(2)</codeph> to pause the program execution for two seconds before destroying the timer. </p> </li> </ol> <codeblock id="GUID-60B88762-E6C3-52F8-8DBC-E77D70B03FAE" xml:space="preserve">#include &lt;time.h&gt;
    13 a Periodic Timer</title><prolog><metadata><keywords/></metadata></prolog><conbody>
       
    14 <p>You can use a periodic timer to provide repeated signals to your process. </p>
       
    15 <p>The following example illustrates a periodic timer with a delay of a second
       
    16 and a repeating interval of ten milliseconds. It also configures a thread
       
    17 function as the timer expiry notification using <codeph>SIGEV_THREAD</codeph>. </p>
       
    18 <p>The following example code performs the following tasks: </p>
       
    19 <ol id="GUID-B648631F-8CC2-5304-B2DC-6A9293385E3E">
       
    20 <li id="GUID-228938F5-3F52-5853-96FC-2556650A617A"><p>Creates a notification
       
    21 function (thread function) that must be invoked after timer expiry. </p> </li>
       
    22 <li id="GUID-7F02B8B1-7AE3-566A-A864-A64B45837C79"><p>Sets the thread priority
       
    23 to <b>255</b> using the thread scheduling parameters (<codeph>struct sched_param</codeph>).
       
    24 This ensures that the thread function has the highest priority when it is
       
    25 invoked as a result of a timer expiry. </p> </li>
       
    26 <li id="GUID-14F0D0AC-65EF-56B6-B82A-EC41C4DDA94F"><p>Creates a timer based
       
    27 on the current system time (<codeph>CLOCK_REALTIME</codeph>) and a notification
       
    28 function (<codeph>struct              sigevent sig</codeph>) that must be
       
    29 invoked when the timer expires. </p> </li>
       
    30 <li id="GUID-0A915D17-5113-563B-9CE4-7A7B7B7D7B23"><p>Defines the input values
       
    31 for <xref href="GUID-344F836B-FAC8-344A-B458-0CEFB714ADEB.dita"><apiname>timer_settime()</apiname></xref>. The key input values are the timer
       
    32 value (<codeph>in.it_value.tv_sec = 1;</codeph>) and the interval (<codeph>in.it_interval.tv_nsec
       
    33 = 100000000;</codeph>). The periodic timer will expire after a second and
       
    34 then invoke the notification function every one-tenth of a second until it
       
    35 is destroyed. </p> </li>
       
    36 <li id="GUID-126A5B20-133D-585A-9FF9-9CB5456D2D71"><p>Starts the periodic
       
    37 timer using <xref href="GUID-344F836B-FAC8-344A-B458-0CEFB714ADEB.dita"><apiname>timer_settime()</apiname></xref>. </p> </li>
       
    38 <li id="GUID-FD1EEA8C-3215-5DBE-BB29-47576DED3955"><p>Uses <codeph>sleep(2)</codeph> to
       
    39 pause the program execution for two seconds before destroying the timer. </p> </li>
       
    40 </ol>
       
    41 <codeblock id="GUID-60B88762-E6C3-52F8-8DBC-E77D70B03FAE" xml:space="preserve">#include &lt;time.h&gt;
    14 #include &lt;stdio.h&gt;
    42 #include &lt;stdio.h&gt;
    15 #include &lt;signal.h&gt;
    43 #include &lt;signal.h&gt;
    16 #include &lt;pthread.h&gt;
    44 #include &lt;pthread.h&gt;
    17 #include &lt;unistd.h&gt;
    45 #include &lt;unistd.h&gt;
    18 #include &lt;errno.h&gt;
    46 #include &lt;errno.h&gt;
    60         timer_delete(timerid);
    88         timer_delete(timerid);
    61         }
    89         }
    62     else
    90     else
    63     printf("timer_create() failed with %d\n", errno);
    91     printf("timer_create() failed with %d\n", errno);
    64     return Ret;
    92     return Ret;
    65     }</codeblock> <p>The output of the above program is: </p> <codeblock id="GUID-313450D1-6766-58C6-933C-49AD55A5D985" xml:space="preserve">Handler entered with value :20 for 1 times
    93     }</codeblock>
       
    94 <p>The output of the above program is: </p>
       
    95 <codeblock id="GUID-313450D1-6766-58C6-933C-49AD55A5D985" xml:space="preserve">Handler entered with value :20 for 1 times
    66 Handler entered with value :20 for 2 times
    96 Handler entered with value :20 for 2 times
    67 Handler entered with value :20 for 3 times
    97 Handler entered with value :20 for 3 times
    68 Handler entered with value :20 for 4 times
    98 Handler entered with value :20 for 4 times
    69 Handler entered with value :20 for 5 times
    99 Handler entered with value :20 for 5 times
    70 Handler entered with value :20 for 6 times
   100 Handler entered with value :20 for 6 times
    71 Handler entered with value :20 for 7 times
   101 Handler entered with value :20 for 7 times
    72 Handler entered with value :20 for 8 times</codeblock> <p> <b>Note:</b> Ideally, in the preceding output the handler must have entered <b>10</b> times. This is not the case on Symbian platform as there is some latency due to the timer emulation solution and the underlying Symbian platform clock resolution. </p> </conbody></concept>
   102 Handler entered with value :20 for 8 times</codeblock>
       
   103 <p> <b>Note:</b> Ideally, in the preceding output the handler must have entered <b>10</b> times.
       
   104 This is not the case on the Symbian platform as there is some
       
   105 latency due to the timer emulation solution and the underlying Symbian platform
       
   106 clock resolution. </p>
       
   107 </conbody></concept>