0

I am trying to learn about timer for posix. I am using the linux man page for an example. The man page create the timer then uses sleep to put the timer to sleep. Why create a timer if you use sleep? Here is the code from the man page

    if (timer_create(clockid, &sev, &timerid) == -1) {
    errExit("timer_create");
}



freq_nanosecs = atoll(argv[2]);
       its.it_value.tv_sec = freq_nanosecs / 1000000000;
       its.it_value.tv_nsec = freq_nanosecs % 1000000000;
       its.it_interval.tv_sec = its.it_value.tv_sec;
       its.it_interval.tv_nsec = its.it_value.tv_nsec;

       /* Sleep for a while; meanwhile, the timer may expire
          multiple times */
 if (timer_settime(timerid, 0, &its, NULL) == -1)
     errExit("timer_settime");


       printf("Sleeping for %d seconds\n", atoi(argv[1]));
       sleep(atoi(argv[1]));
Aaron
  • 4,380
  • 19
  • 85
  • 141
  • Looks you're seriously mistaken about some basic concepts about `std::sleep`'s actual behavior! Check for implementation of asynchronous timers ... – πάντα ῥεῖ Feb 11 '14 at 01:09
  • Can you explain? I got that I misunderstand what is going on. – Aaron Feb 11 '14 at 01:13
  • _'Can you explain'_ Sorry, no! I don't know what's meant with `timer_settime( ...`, and where you've got that from (or the `timerid`). Put a [SCCEE](http://stackoverflow.com/help/mcve) to reproduce your problem (wasn't I already mentioning this to you, maybe for a different question??) – πάντα ῥεῖ Feb 11 '14 at 01:18
  • Looks the sample snippet you've been giving, just uses `sleep()` to show s.th.'s going on for the timer asynchonously ... – πάντα ῥεῖ Feb 11 '14 at 01:20
  • I just used what was in the man pages in linux. I don't know how to do something I try their example. It doesn't seem to work I look it up. If I am out of option I come here. – Aaron Feb 11 '14 at 01:22
  • _'doesn't seem to work'_ is to narrow (vague) to elaborate on your problems (you know!!) – πάντα ῥεῖ Feb 11 '14 at 01:24
  • _'If I am out of option I come here.'_ You'd never ever **really** want to come up asking such questions **here**!! – πάντα ῥεῖ Feb 11 '14 at 01:26
  • Sleep doesn't make sense, because why create the timer if you are going to use sleep. What makes sense if there is a way to check how much time you have left in a while loop. It tried that with timer_gettime, but it returns 0. – Aaron Feb 11 '14 at 01:29
  • Aaron, if you can explain what you're trying to do here I can happily provide you a very constructive answer. – Misha Nasledov Feb 11 '14 at 01:31
  • You're going to `sleep()` in the main thread, and should notice the timer sending events (calling callbacks) asynchonously meanwhile. That's what the sample's all about, OK?? – πάντα ῥεῖ Feb 11 '14 at 01:31

1 Answers1

2

Because if your program doesn't have anything else to do (i.e. no main event loop), then it will exit once it reaches the end of that block. If you comment out the sleep() it'll just exit right away.

You can replace sleep(atoi(argv[1])) with a while(1);

Basically, your program has run out of instructions to run. The timer is typically used for things like signaling operations to happen in an asynchronous manner. For example, you can set the timer up to send your process a signal and set up a signal handler to process that event.

I'd say see timer_create(2) for more clarity but looks like you pasted some code straight out of there.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Misha Nasledov
  • 2,003
  • 1
  • 15
  • 18
  • I copied this straight from the man pages. I thought I mentioned that. How would I use while(1); I want to do while(timer), but am having trouble figuring out if I should use timer_settime or timer_gettime. I tried both and neither work – Aaron Feb 11 '14 at 01:15
  • My point is your program has to *do something* -- sleep or an empty while loop is just a way for it to keep running in the manpage example. You have to use timer_create() to instantiate it and timer_settime() to start it up. – Misha Nasledov Feb 11 '14 at 01:28
  • If you can clarify exactly what you're trying to do, I can provide a more proper answer. You don't need to use sleep() when creating a timer. – Misha Nasledov Feb 11 '14 at 01:31
  • I am using timer_Create() and timer_settime(). I am adding the code to example in my question – Aaron Feb 11 '14 at 01:31
  • Do you really call timer_settime() twice? Is the struct itimerspec its initialized in the first call? Anyway, I still don't see what you're trying to do. This is a skeleton example from the manpages. – Misha Nasledov Feb 11 '14 at 01:40
  • I only call setttime() once. I accidentally put it in twice. What I am trying to do is after I set the timer have the thread pause for x number of seconds. settime() arms the timer, so how do I put the thread to sleep while the the timer is going? – Aaron Feb 11 '14 at 01:50
  • Found an example at http://stackoverflow.com/questions/5740954/problem-in-timers-and-signal. Thank anyway – Aaron Feb 11 '14 at 02:15