Thread: making my program sleep / wait / delay...

  1. #1
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122

    making my program sleep / wait / delay...

    Hi, basically, I am looking to put a pause into my program for about 1 second before doing a read. How can i do this??? i know is c, its just sleep(ms); with unistd.h, but how to do it in c++???

  2. #2
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    you can use Sleep(ms); with #include <windows.h>

    edit:
    Here is more info on it: http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > i know is c, its just sleep(ms); with unistd.h, but how to do it in c++???
    This is an API issue, not a language issue.

    The answer depends on the OS and compiler you're using more than the language.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122
    OK! ...So, how can i ensure that my code will work on both unix/linux and windows based systems, if they both use different libraries???

    is it something like

    #IFDEF

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well the sleep function (which waits in units of a second) is a POSIX-1 function which nearly all 32-bit operating systems support.

    It's in unistd.h on unix-like operating systems, but probably in something else on windows.
    So you might need conditional compilation around a header file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New, making a survey program
    By shaffer in forum C++ Programming
    Replies: 18
    Last Post: 12-01-2006, 11:36 AM
  2. why do we require sleep?
    By jinx in forum A Brief History of Cprogramming.com
    Replies: 43
    Last Post: 07-14-2004, 08:21 AM
  3. delay or sleep?
    By GanglyLamb in forum C Programming
    Replies: 17
    Last Post: 12-12-2002, 11:46 PM
  4. How to make a program wait for an event?
    By DRoss in forum C++ Programming
    Replies: 0
    Last Post: 09-10-2001, 01:13 PM
  5. How can I make a program wait for an event?
    By DRoss in forum C++ Programming
    Replies: 0
    Last Post: 09-06-2001, 09:28 AM