Thread: Best method for console timing system?

  1. #1
    Banned
    Join Date
    Aug 2010
    Location
    USA! USA! USA!
    Posts
    19

    Best method for console timing system?

    In C++ whats the best way tofor me to create an object and have it wait 1 minute before echoing a message? Please note it will be in a while loop(duh or else program will close/end).

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    ctime (time.h) - C++ Reference

    Have a look at that reference (time.h). In this case, look at clock() time() and difftime().

  3. #3
    Registered User jdragyn's Avatar
    Join Date
    Sep 2009
    Posts
    96
    One option, which is portable but a Bad Idea in most cases, is to use the clock() and/or time() functions to busy wait until a minute has elapsed. The issue is that this approach uses 100% CPU usage (or as much as the OS is willing to hand over to your program anyway).

    The other options depend on which OS you are using and are not portable. In Windows there is Sleep(). I'm not sure about *nix. This is far preferable to a busy wait (busy because it keeps checking the value of time() or checking how many clock()'s have elapsed, or however you decide to structure it). Not only does it not stress the CPU, but it makes your program play nicer with other programs.
    C+/- programmer extraordinaire

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to model a system?
    By ovid in forum C++ Programming
    Replies: 1
    Last Post: 03-05-2010, 09:18 AM
  2. Reliable UDP communication for merging events in a distributed system
    By dwks in forum Networking/Device Communication
    Replies: 1
    Last Post: 12-26-2009, 05:36 PM
  3. Simple File System in C
    By tsiknas in forum Linux Programming
    Replies: 2
    Last Post: 12-20-2009, 01:37 PM
  4. measuring system resources used by a function
    By Aran in forum C Programming
    Replies: 1
    Last Post: 03-13-2006, 05:35 PM
  5. My Timing System
    By jmd15 in forum Windows Programming
    Replies: 4
    Last Post: 01-01-2006, 11:43 PM