2

I want to write a program to simulate a cafeteria in which a customer enters the cafeteria every 10 ~ 50 seconds. So I wanted a function to be called every 10~50 seconds for displaying the menu card which is present in the function to be called

Community
  • 1
  • 1
user2219913
  • 582
  • 2
  • 5
  • 13
  • 1
    Do you need other stuff happening in that 10-50 seconds, or does it just wait on the person entering the cafeteria? – chris Mar 28 '13 at 13:32
  • I am beginner in c++ and I searched in net but i didn't find nothing – user2219913 Mar 28 '13 at 13:32
  • I have a method that I want to call every 50 secondes – user2219913 Mar 28 '13 at 13:33
  • 4
    But what I'm saying is that methods to do this can be simplified if all you're doing is sitting there for 50 seconds and then calling the function. If you're not doing anything else in that time, you can just loop a 50-second sleep and the function call. – chris Mar 28 '13 at 13:38
  • @chris +1 for asking whether he needs polling approach or asynchronous timer. – andre_lamothe Mar 28 '13 at 13:39
  • 1
    It sounds like you want the simulated customers to arrive at random intervals between 10 and 50 seconds. Do the arrivals follow a Poisson distribution, perhaps? And--seriously--do you have *any* work to show us at this point? – David Gorsline Mar 28 '13 at 13:51
  • Related: [Calling a function every 1 second (precisely)](https://stackoverflow.com/q/50136540/11942268). – stackprotector Oct 14 '21 at 12:00

4 Answers4

6

The Boost library provides for this in Boost.Asio, and explicitly covers this in its tutorials:

  • Synchronous timer, i.e. waiting until the timer expires.

  • Asynchronous timer, i.e. continuing with your program and having the callback function invoked when the timer expires.

If you didn't find the Boost library when searching the web for C++, your google-fu is weak. ;-)

DevSolar
  • 67,862
  • 21
  • 134
  • 209
  • Just curious, will this work if I put it in a game loop? `async timer`. Like call a certain function every 5 seconds. – majidarif Mar 28 '16 at 10:38
  • @majidarif: Without ever having used Boost.Asio myself and without having looked at the docs, I will answer with an unqualified "yes". Calling a function on fixed intervals is what such libs are FOR. – DevSolar Mar 28 '16 at 10:42
  • oh okay, I was concerned that it might schedule a timer every time it gets called inside the loop. like instead of calling something every 5 seconds, it calls something every loop after 5 seconds. – majidarif Mar 28 '16 at 10:56
  • @majidarif: Well, that depends on how you set it up, doesn't it? – DevSolar Mar 28 '16 at 10:57
4

In your main-loop, get the time and compare it to the last time you called the function (initialized to "now" when declaring it). If it's larger than your interval then call your function and set the "last-time-called" variable to the current time.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

There are two approaches: 1.Asynchronous 2. Synchronous

  1. Assuming that you are using Win32, C++. You can use Win32 API SetTimer

    UINT_PTR timerid = SetTimer(NULL, 0, milliseconds, &callback);

  2. If you would like a Polling Approach You would better use something like that

      for(;;) 
        {
    
        Say_Hello();
        // Sleep for 50*1000ms
        Sleep(50000);
    
        }
    
andre_lamothe
  • 2,171
  • 2
  • 41
  • 74
  • 1
    You can make your own in standard C++ as well. You don't have to use the winapi. – chris Mar 28 '13 at 13:35
  • @us2012 it depends whether sleeps in ms or in seconds... that's why I commented as 50*1000ms...or you can write Sleep(50000)... – andre_lamothe Mar 28 '13 at 13:52
  • Yeah. My question is why you write it as a `for` loop instead of `sleep(50000)`. For times on this order of magnitude, it may not matter, but for smaller times, the results may be different depending on whether you `sleep` in one block or split into multiple ones. – us2012 Mar 28 '13 at 14:07
  • @Ahmed : thanks brother. But, how can I use the SetTimer API. I have written this code test : voif f() { printf ("hello");} int main() {SetTimer(NULL,0, 10000, (TIMERPROC) &f);} But it does not work. thx – user2219913 Mar 28 '13 at 14:26
  • 2
    @user2219913: "It does not work." is a statement that immediately disqualifies from any more helpful comments... – DevSolar Mar 28 '13 at 15:15
1

For a timer more cross-platform and utilizing the c++ STL, take a look at C++ Cross-Platform High-Resolution Timer.

From there, simply create and call a function in a loop every 10 ~ 50 seconds using two of those cross-platform timers mentioned above.

Community
  • 1
  • 1
Mushy
  • 2,535
  • 10
  • 33
  • 54