Re: [Dev-C++] sleep() for Windows XP
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Adam J. <aj...@i-...> - 2007-07-24 15:39:43
|
>> You Wrote:
>>But it also eats your cpu cycles... Sleep doest consume CPU.
Please reserve your comments to information that is accurate. You are giving
the wrong information. I don't know any CPU Instruction that does not
require cpu cycles.
Sleep() is not some magical command, nor a cpu instruction. It is a function
call just like any other function call. It takes a value (DWORD) and returns
VOID. It's prototype declaration is in windows.h.
If you take the time to investigate you will notice the assembler code that
gets produced when you make this call. Better yet you could have just read
the MSDN entry on microsoft's website and it would have told you how this
function works. Functionally it works the same way as the function I wrote.
It checks the clock ticks and compares them to see if the require time has
lapse.
The major difference is that my function is platform independent, and it
does not require preprocessor commands. As you know, windows.h is only
available on a windows platform. The function I wrote, requires only time.h
which is a c++ library.
I will even take this one step further. If you compile these two programs,
the sizes of the files are the same (16k), and the assembler code that is
produced is almost Identical.
Program 1:
//Source code headers
#include <time.h>
//Main program function
int main()
{
//sleep command
unsigned int clockTicks;
clock_t ticksReached;
clockTicks = 3000;
ticksReached = clockTicks + clock();
while (ticksReached > clock());
//Exit program
return 0;
}
Program 2:
//Source code headers
#include <windows.h>
//Main program function
int main()
{
//sleep command
Sleep(300);
//Exit program
return 0;
}
- Adam Jones
-----Original Message-----
From: matheus ribeiro [mailto:mfr...@gm...]
Sent: Tuesday, July 24, 2007 12:32 AM
To: Adam Jones
Cc: Jamiil Abduqadir; Dev Cpp MailingLinst
Subject: Re: [Dev-C++] sleep() for Windows XP
But it also eats your cpu cycles... Sleep doest consume CPU.
2007/7/23, Adam Jones <aj...@i-...>:
> Just in case you are interested, here is a platfrom independent way to
cause
> your program to sleep without using sleep() or MS version Sleep(). It also
> avoids preprocessors commands.
>
> #include <time.h>
>
> Int main()
> {
>
> unsigned int clockTicks; //Stores the
amount
> of time you want to pause execution in clock ticks
> clock_t ticksReached; //Stores the
amount
> of clock ticks
>
> clockTicks = 3000; //set pause to 3 seconds
> ticksReached = clockTicks + clock(); //get number of
> clocks tick since program started then add the addition wait time
> while (ticksReached > clock()); //loop until current clock
> ticks equal the ticks needed to wait
>
> return 0;
> }
>
> It is more code, but it is platfrom independent and free of preprocessor
> commands.....
>
> -Adam Jones
>
>
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf Of Adam
Jones
> Sent: Monday, July 23, 2007 10:24 PM
> To: 'Jamiil Abduqadir'; 'Dev Cpp MailingLinst'
> Subject: Re: [Dev-C++] sleep() for Windows XP
>
> >>You wrote:
> >> Does anyone know where I can find a source code for such service, so
that
> it is a portable sleep() function?
>
> Why do you feel the need to cause your program to sleep(). There may be
> other ways of pausing executive without using a platform dependent
command.
> Or using #defines. If you could provide more details, that may be helpful.
>
> -Adam Jones
>
> From: dev...@li...
> [mailto:dev...@li...] On Behalf Of Jamiil
> Abduqadir
> Sent: Monday, July 23, 2007 3:37 PM
> To: Dev Cpp MailingLinst
> Subject: [Dev-C++] sleep() for Windows XP
>
> Using GCC, I developed some small apps under Linux, now I am trying to
port
> this apps to MSWinXP, but there are some Unix functions that are not
> availableunder MS, sleep() is one of them. Does anyone know where I can
> find a source code for such service, so that it isa portable sleep()
> function?
>
>
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc.
> Still grepping through log files to find problems? Stop.
> Now Search log events and configuration files using AJAX and a browser.
> Download your FREE copy of Splunk now >> http://get.splunk.com/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc.
> Still grepping through log files to find problems? Stop.
> Now Search log events and configuration files using AJAX and a browser.
> Download your FREE copy of Splunk now >> http://get.splunk.com/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|