Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I posted this answer to illustrate a quite different approach to the "obvious" one, in the hope that someone discovers it to be exactly what they need. I didn't expect it to be selected as the best answer! Treat this solution with caution, because there are potential dangers and concurrency issues...</p> <p>You can use the <a href="http://linux.die.net/man/2/setitimer" rel="nofollow noreferrer">setitimer()</a> system call to have a SIGALRM (alarm signal) sent to your program after a specified number of milliseconds. Signals are asynchronous events (a bit like messages) that interrupt the executing program to let a signal handler run.</p> <p>A set of default signal handlers are installed by the OS when your program begins, but you can install a custom signal handler using <a href="http://linux.die.net/man/2/sigaction" rel="nofollow noreferrer">sigaction()</a>.</p> <p>So all you need is a single thread; use global variables so that the signal handler can access the necessary information and send off a new packet or repeat the last packet as appropriate.</p> <p>Here's an example for your benefit:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;signal.h&gt; #include &lt;sys/time.h&gt; int ticker = 0; void timerTick(int dummy) { printf("The value of ticker is: %d\n", ticker); } int main() { int i; struct sigaction action; struct itimerval time; //Here is where we specify the SIGALRM handler action.sa_handler = &amp;timerTick; sigemptyset(&amp;action.sa_mask); action.sa_flags = 0; //Register the handler for SIGALRM sigaction(SIGALRM, &amp;action, NULL); time.it_interval.tv_sec = 1; //Timing interval in seconds time.it_interval.tv_usec = 000000; //and microseconds time.it_value.tv_sec = 0; //Initial timer value in seconds time.it_value.tv_usec = 1; //and microseconds //Set off the timer setitimer(ITIMER_REAL, &amp;time, NULL); //Be busy while(1) for(ticker = 0; ticker &lt; 1000; ticker++) for(i = 0; i &lt; 60000000; i++) ; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload