Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 20th, 2006, 12:27 PM   #11
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>Only know how to do it in Windows and Linux
Have your implementation define a name depending on the build. Then you don't have to know the tricks, or the exceptions (there are several for Linux):
#if IS_LINUX
  #include <unistd.h>
  #define delay(x) sleep(x)
#elif IS_WINDOWS
  #include <windows.h>
  #define delay(x) Sleep(x * 1000)
#else
  #define delay(x) std::cout << "Unknown OS" << std::endl;
#endif
Then on Linux you could build like so:
gcc -D IS_LINUX main.c
You'll notice that a lot of open source code does this, especially with assumptions about the standard library.
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old Nov 21st, 2006, 11:32 AM   #12
v0id
Hobbyist Programmer
 
Join Date: Apr 2006
Posts: 155
Rep Power: 3 v0id is on a distinguished road
Oh.. Thanks a lot for the tip, Narue!
__________________
-- v0id
v0id is offline   Reply With Quote
Old Nov 24th, 2006, 7:54 AM   #13
Lesliect6
Programmer
 
Join Date: Aug 2005
Posts: 68
Rep Power: 4 Lesliect6 is on a distinguished road
The delay function in Windows is simple to write, you only need the <time.h> library, which should be included with your compiler. Then, define the function delay :
void delay(int seconds)
{
  clock_t endwait;
  endwait = clock () + seconds * CLK_TCK ;
  while (clock() < endwait) {}
}
Lesliect6 is offline   Reply With Quote
Old Nov 24th, 2006, 8:29 AM   #14
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>The delay function in Windows is simple to write, you only need the <time.h> library
Of course, a busy loop is extremely antisocial in a multitasking OS. If you're assuming Windows, then just use Sleep.

>which should be included with your compiler
Which will be included with your compiler unless you're on a freestanding implementation. And if you're on a freestanding implementation, you probably don't need help writing a delay function.

>CLK_TCK
The CLOCKS_PER_SEC macro is standard and does exactly the same thing. In fact, in every implementation I've seen that defines CLK_TCK, it's done like this:
#define CLK_TCK CLOCKS_PER_SEC
Why sacrifice portability when you don't have to?
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old Nov 24th, 2006, 8:30 AM   #15
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,254
Rep Power: 5 grumpy will become famous soon enough
The problem with that function, Lesliect6, is that the while loop thrashes and therefore starves other programs (and other threads within your program, if it is multithreaded) of processor time. It will therefore have a noticeable effect on the performance of the whole system --- and any other programs running at the same time.

The Sleep(x*1000) described by Narue will introduce a delay, without starving other processes or threads of CPU time. (Incidentally, the header file that declares Sleep() in the win32 API is <winbase.h>).

One approach, sometimes used in addition to the one described by Narue, goes something like;
#include "id_sys.h"

#if IS_LINUX
  #include <unistd.h>
  #define delay(x) sleep(x)
#elif IS_WINDOWS
  #include <windows.h>
  #define delay(x) Sleep(x * 1000)
#else
  #define delay(x) std::cout << "Unknown OS" << std::endl;
#endif
where id_sys.h includes macros to detect the compiler and target system. For example;
#if defined (__MSDOS__) && !defined(__GNUG__)
#define IS_MSDOS
#elif defined(_MSC_VER) || defined(__BCPLUSPLUS__) || defined(__WIN32__)
#define IS_WINDOWS
#elif defined(__vax__)
#define IS_VAX
#endif
This technique works by using predefined macros that are associated with each compiler. The down side is that compiler specific macros change quite significantly between compilers, and sometimes even between compiler versions. The benefit is that, if the code supports your compiler and target system, there is less need to reconfigure build scripts. Usually, I prefer to configure build scripts, but this is an alternate option.
grumpy is offline   Reply With Quote
Old Nov 24th, 2006, 8:33 AM   #16
Lesliect6
Programmer
 
Join Date: Aug 2005
Posts: 68
Rep Power: 4 Lesliect6 is on a distinguished road
Quote:
>CLK_TCK
The CLOCKS_PER_SEC macro is standard and does exactly the same thing. In fact, in every implementation I've seen that defines CLK_TCK, it's done like this:
Code:

#define CLK_TCK CLOCKS_PER_SEC

Why sacrifice portability when you don't have to?
Why, thank you I didn't know that.
Lesliect6 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Combining languages titaniumdecoy Other Programming Languages 12 Jul 13th, 2006 3:03 PM
Compiling Maverik 6.2 (from C) megamind5005 C 16 May 3rd, 2006 6:41 PM
libraries matko C 1 Jan 22nd, 2006 3:12 PM
Jackpot game zorin Visual Basic 3 Jun 10th, 2005 2:19 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 5:12 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:46 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC