Symbian3/PDK/Source/GUID-B4039418-6499-555D-AC24-9B49161299F2.dita
changeset 1 25a17d01db0c
child 3 46218c8b8afa
equal deleted inserted replaced
0:89d6a7a84779 1:25a17d01db0c
       
     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 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
       
    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;
       
    14 #include &lt;stdio.h&gt;
       
    15 #include &lt;signal.h&gt;
       
    16 #include &lt;pthread.h&gt;
       
    17 #include &lt;unistd.h&gt;
       
    18 #include &lt;errno.h&gt;
       
    19 static int i = 0;
       
    20 
       
    21 //Thread function to be invoked when the periodic timer expires
       
    22 void sighler (union sigval val)
       
    23     {
       
    24     printf("Handler entered with value :%d for %d times\n", val.sival_int, ++i);
       
    25     }
       
    26 int main()
       
    27     {
       
    28     int Ret;
       
    29 
       
    30     pthread_attr_t attr;
       
    31     pthread_attr_init( &amp;attr );
       
    32 
       
    33     struct sched_param parm;
       
    34     parm.sched_priority = 255;
       
    35     pthread_attr_setschedparam(&amp;attr, &amp;parm);
       
    36 
       
    37     struct sigevent sig;
       
    38     sig.sigev_notify = SIGEV_THREAD;
       
    39     sig.sigev_notify_function = sighler;
       
    40     sig.sigev_value.sival_int =20;
       
    41     sig.sigev_notify_attributes = &amp;attr;
       
    42 
       
    43     //create a new timer.
       
    44     timer_t timerid;
       
    45     Ret = timer_create(CLOCK_REALTIME, &amp;sig, &amp;timerid);
       
    46     if (Ret == 0)
       
    47         {
       
    48         struct itimerspec in, out;
       
    49         in.it_value.tv_sec = 1;
       
    50         in.it_value.tv_nsec = 0;
       
    51         in.it_interval.tv_sec = 0;
       
    52         in.it_interval.tv_nsec = 100000000;
       
    53         //issue the periodic timer request here.
       
    54         Ret = timer_settime(timerid, 0, &amp;in, &amp;out);
       
    55         if(Ret == 0)
       
    56             sleep(2);
       
    57         else
       
    58             printf("timer_settime() failed with %d\n", errno);
       
    59         //delete the timer.
       
    60         timer_delete(timerid);
       
    61         }
       
    62     else
       
    63     printf("timer_create() failed with %d\n", errno);
       
    64     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
       
    66 Handler entered with value :20 for 2 times
       
    67 Handler entered with value :20 for 3 times
       
    68 Handler entered with value :20 for 4 times
       
    69 Handler entered with value :20 for 5 times
       
    70 Handler entered with value :20 for 6 times
       
    71 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>