![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
>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 gcc -D IS_LINUX main.c
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#12 |
|
Hobbyist Programmer
Join Date: Apr 2006
Posts: 155
Rep Power: 3
![]() |
Oh.. Thanks a lot for the tip, Narue!
__________________
-- v0id
|
|
|
|
|
|
#13 |
|
Programmer
Join Date: Aug 2005
Posts: 68
Rep Power: 4
![]() |
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) {}
} |
|
|
|
|
|
#14 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
>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
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#15 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,254
Rep Power: 5
![]() |
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 #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 |
|
|
|
|
|
#16 | |
|
Programmer
Join Date: Aug 2005
Posts: 68
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |