Thread: generic delay() function

  1. #1
    EOF
    Join Date
    Aug 2005
    Location
    Constanta, RO, Europe
    Posts
    46

    Question generic delay() function

    does anybody know, or know to code a function that makes the program halt for some time? like dos.h -> delay(int), just that i wished it was cross-platform.

    i am working on a program that implies encrypting and decrypting data, and to prevent "users" from testing many keys with a file to find the right one, i wanted to set a one-second delay before the actual process.

  2. #2
    .
    Join Date
    Nov 2003
    Posts
    307
    This is an old-fashioned timing loop.

    You are MUCH better off using OS-specific library routines. And doing a conditonal compile. This will suck up CPU for no good reason. In DOS it did not matter that much. Nowadays it does.


    This waits a minimum of 1 second, probably more, if the seconds argument is 1. It also hogs the cpu.

    Code:
    #include <time.h>
    #include <stdlib.h>
    
    void delay(time_t seconds)
    {
        time_t start=time(NULL);
        time_t end=0;
        seconds++;  /* account for partial second  */
        for(;;)
        {
          end=time(NULL);
          if(seconds + end >= start) break;       
        }
    }

  3. #3
    EOF
    Join Date
    Aug 2005
    Location
    Constanta, RO, Europe
    Posts
    46
    i knew this variant, i was just wondering whether there was something else

    well, i think i'll end up with precompiler options.
    btw, how do you know whether you're doing a compile under windows or linux?

    Code:
    #if (defined(windows) || defined(dos))
        #include <dos.h>
        #define DELAY(x) delay(x)
      #else if defined(linux)
               #include <linux.h>
               #define DELAY(x) linux-equivalent-of-delay(x)
             #else error Only for Linux or DOS/Windows
             #endif
      #endif
    things in italics are things i don't know. maybe you can help.
    Last edited by moonlord; 08-08-2005 at 10:53 AM.

  4. #4
    .
    Join Date
    Nov 2003
    Posts
    307
    Here's a start on what you want:

    Code:
    #ifdef  _WIN32
       #include <dos.h>
       #define DELAY(x) wait(x)
    #else 
       #include <unistd.h>
       #define DELAY(x) sleep(x)
    #endif

  5. #5
    EOF
    Join Date
    Aug 2005
    Location
    Constanta, RO, Europe
    Posts
    46
    my compiler (borland c) doesn't seem to have a "wait" function, it's got a "delay" function instead, which they say it's "calibrated to 1ms". this makes me think it's just the infinite-for version.

    1. i'm not using a win32 compiler, so that needs some adjustments.
    2. unistd.h is supposed to be a cross-OS library? just asking because i don't have it.
    3. how can you make visual studio.net 2002 to make a win16 program/project? i haven't managed.

  6. #6
    .
    Join Date
    Nov 2003
    Posts
    307
    Quote Originally Posted by moonlord
    my compiler (borland c) doesn't seem to have a "wait" function, it's got a "delay" function instead, which they say it's "calibrated to 1ms". this makes me think it's just the infinite-for version.

    1. i'm not using a win32 compiler, so that needs some adjustments.
    2. unistd.h is supposed to be a cross-OS library? just asking because i don't have it.
    3. how can you make visual studio.net 2002 to make a win16 program/project? i haven't managed.
    1. download one for free - try DevC++. The FAQ, etc:
    http://www.bloodshed.net/dev/devcpp.html

    2. unistd.h is purely unix, since you want to compile the code over there, you need the header file.

    3. you can't AFAIK.

  7. #7
    EOF
    Join Date
    Aug 2005
    Location
    Constanta, RO, Europe
    Posts
    46
    is there anything that's defined only for DOS and something else only for UNIX/Linux? like:

    Code:
    #ifdef _DOS
     #include <dos.h>
     #define DELAY(n) delay(n)
    #else
       #ifdef _WIN32
        #include <dos.h>
        #define DELAY(n) wait(n)
       #else
          #ifdef _UNIX_OR_LINUX
           #include <unistd.h>
           #define DELAY(n) sleep(n)
          #else
           #error This program is designed only for DOS/Windows or UNIX/Linux
          #endif
       #endif
    #endif
    where _DOS and _UNIX_OR_LINUX are the things I'm asking for.
    Last edited by moonlord; 08-09-2005 at 06:43 AM.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    EOF
    Join Date
    Aug 2005
    Location
    Constanta, RO, Europe
    Posts
    46
    10x all of you. i've solved the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generic function
    By vin_pll in forum C++ Programming
    Replies: 26
    Last Post: 02-04-2009, 07:36 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM