4
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <time.h>

using namespace std;
using namespace System;

void wait ( int seconds )
{
  clock_t endwait;
  endwait = clock() + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}
void timer()
{
    int n;
    printf ("Start\n");
    for (n=10; n>0; n--) // n = time
    {
        cout << n << endl;
        wait (1); // interval (in seconds).
    }
    printf ("DONE.\n");
    system("PAUSE");
}
int main ()
{
    timer();
    cout << "test" << endl; // run rest of code here.}

  return 0;
}

I'm trying to create a timer in C++ which would run in the background. So basically if you'd look at the 'main block' I want to run the timer (which is going to count down to 0) and at the same time run the next code, which in this case is 'test'.

As it is now the next line of code won't be run until the timer has finished. How do I make the timer run in the background?

Thanks for your help in advance!

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
Rob
  • 175
  • 3
  • 3
  • 12
  • What operating system? These already exist. – Duck Mar 19 '12 at 20:46
  • Oh, never mind, I see what you are trying to do. – Duck Mar 19 '12 at 20:52
  • Are you sure you want the timer code to run at the same time as your other code? Because that means you'll have to learn multi-threading. Any time you run two bits of code at the same time in the same process, there are a variety of rules you have to follow to keep them from stepping on each other's toes. There are lots of ways you could do it without running two bits of code at the same time, including an event loop or using Windows messages. – David Schwartz Mar 19 '12 at 20:53
  • Basically what I want to do is tell the timer to start running and after I told it to start running I want to run the rest of the code without having to wait until the timer has finished. – Rob Mar 19 '12 at 20:57
  • In Visual Basic this was fairly easy to accomplish though. Aren't there any other (simple) ways in accomplishing this? – Rob Mar 19 '12 at 20:58
  • How complex is the code that's to be run when the timer expires? How much does it interact with the rest of the system? – Kerrek SB Mar 19 '12 at 21:05
  • Well, it has to register pressed keys so yeah it's pretty important for the code to run continuously. – Rob Mar 19 '12 at 21:12
  • As a side note, generally mixing `iostream` IO with `stdio` IO can result in unexpected output. – Mark B Mar 19 '12 at 21:16
  • Well, in this case it doesn't make that much of a difference. But I'll keep in mind they don't go along for the future. Thanks. http://www.linuxquestions.org/questions/programming-9/iostream-or-stdio-111213/ – Rob Mar 19 '12 at 21:21

1 Answers1

4

C++11. Should work with VS11 beta.

#include <chrono>
#include <iostream>
#include <future>

void timer() {
    std::cout << "Start\n";
    for(int i=0;i<10;++i)
    {
        std::cout << (10-i) << '\n';
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }
    std::cout << "DONE\n";
}
int main ()
{
    auto future = std::async(timer);
    std::cout << "test\n";
}

If the operation performed in timer() takes significant time you can get better accuracy like this:

void timer() {
    std::cout << "Start\n";
    auto start = std::chrono::high_resolution_clock::now();
    for(int i=0;i<10;++i)
    {
        std::cout << (10-i) << '\n';
        std::this_thread::sleep_until(start + (i+1)*std::chrono::seconds(1));
    }
    std::cout << "DONE\n";
}
bames53
  • 86,085
  • 15
  • 179
  • 244
  • The accuracy (if it is even important since OP only has a loop with 10) will decrease as this is run since it doesn't account for how long it takes `cout` to log to console. – Joe Mar 19 '12 at 21:04
  • I'm not looking for extreme accuracy indeed, however I can't use this code since I'm running VB10 and I can't include for some reason or another. Thanks for your help though, bames! (Mind you I have little C++ knowledge!) – Rob Mar 19 '12 at 21:15
  • @Rob VS didn't have `` before 11. Nor `` IIRC. You can get the VS11 beta free, if you're not stuck on 10 for other reasons. – bames53 Mar 19 '12 at 21:46
  • @Joe I added a version that will take into account how much time the operation begin performed takes, as long as it isn't more than the one second time interval. – bames53 Mar 19 '12 at 21:52
  • Any idea how to make 'std::this_thread::sleep_until' work in xcode? I'm getting 'No member named this_thread in namespace std'. – Idan Moshe Dec 18 '13 at 14:43
  • @IdanMoshe you need to set the standard library to libc++ in the build settings for your project. – bames53 Dec 18 '13 at 20:48
  • @bames53 'std::cout' -> no member named cout (i replaced it with printf), I changed C++ Standard Library to libc++ as you said, and I also changed C++ Language Dialect to compiler default and C Language Dialect to compiler default. Both in project and target and still not recognized. – Idan Moshe Dec 19 '13 at 10:53
  • @IdanMoshe did you `#include `? If so it sounds like something's gotten screwed up with your project. Try starting a new C++ command line project. – bames53 Dec 20 '13 at 00:50