473,462 Members | 1,070 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Making my own Sleep() function

3
Hello

How can I make my own Sleep function, without using while() and comparing time passed?
I tried to find something useful on google, but I wasn't able to find anything useful.

----
Nazwa
Jul 27 '07 #1
11 12805
Hello

How can I make my own Sleep function, without using while() and comparing time passed?
I tried to find something useful on google, but I wasn't able to find anything useful.

----
Nazwa
Just Call a delay function with a parameter.
make the function recursive by decrementing the parameter.
Give exit status as parameter value as Zero to come out.
For Ex:
Expand|Select|Wrap|Line Numbers
  1. void delay(20); // function call
  2.  
  3. //Delay Function
  4. void delay(int i)
  5. {
  6.  if (i == 0)
  7.  return;
  8.  delay(i-10);  // give the decrement as 1 or 10 or 100 as convenient
  9. }
  10.  
Jul 27 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
Please don't do this:
//Delay Function
void delay(int i)
{
if (i == 0)
return;
delay(i-10); // give the decrement as 1 or 10 or 100 as convenient
}
The delay() function recursively calls itself. This will lead to a crash when the stack memory is exhausted.

With a 3.8GHZ processor, you will need a very big loop indeed to delay even a small amount.

What is wrong with using the OS sleep function in the first place??
Jul 27 '07 #3
tnga
27
Just Call a delay function with a parameter.
make the function recursive by decrementing the parameter.
Give exit status as parameter value as Zero to come out.
For Ex:
Expand|Select|Wrap|Line Numbers
  1. void delay(20); // function call
  2.  
  3. //Delay Function
  4. void delay(int i)
  5. {
  6.  if (i == 0)
  7.  return;
  8.  delay(i-10);  // give the decrement as 1 or 10 or 100 as convenient
  9. }
  10.  
LOL, that's cool!!!!
Jul 27 '07 #4
seforo
60
Why do you want to reinvent the wheel. Sleep is already there. You can simply do this
Expand|Select|Wrap|Line Numbers
  1. #include <unistd.h>
  2. int main()
  3. {
  4.   sleep(4);
  5.   return 0;
  6. }
  7.  
Jul 27 '07 #5
Nazwa
3
Problem with sleep is that It takes seconds as parameter, and I need miliseconds. I can always make my own function by creating while loop which runs for requested time, but it uses full CPU. I tried with unistd.h but it's not working under BCB and C::B.
Do you have any other idea how to do it? I've seen something like that made in short asm code long, long time ago, but can't find it again.
Jul 28 '07 #6
Problem with sleep is that It takes seconds as parameter, and I need miliseconds. I can always make my own function by creating while loop which runs for requested time, but it uses full CPU. I tried with unistd.h but it's not working under BCB and C::B.
Do you have any other idea how to do it? I've seen something like that made in short asm code long, long time ago, but can't find it again.
Expand|Select|Wrap|Line Numbers
  1. //function call
  2. mysleep(50);
  3.  
  4. //function
  5. void mysleep(int i)
  6. {
  7.   for(int n=1;n<i;n++);
  8. }
  9.  
I think this is what you are looking for.
Jul 30 '07 #7
JonLT
41
Expand|Select|Wrap|Line Numbers
  1. #include <ctime>
  2.  
  3. void mySleep(int sleepTime)
  4. {
  5.    int curTime = clock(); //get the current time
  6.    while(clock() - curTime < sleepTime){} //wait until the time has passed
  7. }
  8.  
the function takes milliseconds as argument. It works by first getting the current time, and the subtracting that from the real current time, and when the result of that is bigger than the passed value, the time has passed.
Jul 30 '07 #8
Nazwa
3
Expand|Select|Wrap|Line Numbers
  1. #include <ctime>
  2.  
  3. void mySleep(int sleepTime)
  4. {
  5.    int curTime = clock(); //get the current time
  6.    while(clock() - curTime < sleepTime){} //wait until the time has passed
  7. }
  8.  
the function takes milliseconds as argument. It works by first getting the current time, and the subtracting that from the real current time, and when the result of that is bigger than the passed value, the time has passed.
It works, but it uses 100% CPU, and point is to make it to use much less.
After all, I think there is nothing more to deal with.

Thanks for help.
Aug 2 '07 #9
RRick
463 Expert 256MB
You're back to using a system call for your timeouts.

On Unix there's something called nanosleep. It gets around the "seconds" limitation of the sleep command. There's also "gettimeofday" which returns time with sub-second accuracy.

I'm not sure what Windows uses, but I'm pretty sure it has one, too.
Aug 2 '07 #10
Banfa
9,065 Expert Mod 8TB
You're back to using a system call for your timeouts.

On Unix there's something called nanosleep. It gets around the "seconds" limitation of the sleep command. There's also "gettimeofday" which returns time with sub-second accuracy.

I'm not sure what Windows uses, but I'm pretty sure it has one, too.
The Sleep function in Windows already takes milliseconds as it's parameter unit
Aug 3 '07 #11
Akai
1
It works, but it uses 100% CPU, and point is to make it to use much less.
After all, I think there is nothing more to deal with.

Thanks for help.

Hello!

I had the same problem using a function like that, but I think that I found a solution:


Expand|Select|Wrap|Line Numbers
  1. #include <time.h>
  2.  
  3. void staticSleep(int frequency)
  4. {
  5.     clock_t goal = ( frequency * 1000 ) + clock();
  6.     while (goal > clock())
  7.       Sleep( 1 );
  8. }
Where "frequency" is the time in seconds that you want to wait.

I hope that it is what you looking for.
Aug 10 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: K Balusu | last post by:
Hi all, We have to develop a small engine which the client uses. It supposed to work like this. Our engine resides in a frame (frameA) which will be loaded only once and it provides set of...
11
by: Yeounkun, Oh | last post by:
Hello. Sleep (x) function make a process sleep during x seconds. but, how to sleep during milliseconds... Pls. help me. Thank you. Regards.
10
by: Alfonso Morra | last post by:
Hi, I need help witht he sleep function as follows. I need to be write som code to do the ff: 1. creates a new thread 2. (in the new thread), Sleep for x milliseconds 3. (in the new thread),...
5
by: Sinan Nalkaya | last post by:
hello, i need a function like that, wait 5 seconds: (during wait) do the function but function waits for keyboard input so if you dont enter any it waits forever. i tried time.sleep() but when...
17
by: OlafMeding | last post by:
Below are 2 files that isolate the problem. Note, both programs hang (stop responding) with hyper-threading turned on (a BIOS setting), but work as expected with hyper-threading turned off. ...
2
by: fltcpt | last post by:
After reading the many posts on the newsgroup, I still disagree with the people who claim you will never need a sleep(), or that a sleep() is a bad idea, or that sleep() does not fit in the event...
2
by: The Last Ottoman | last post by:
Hi, I'm a student in Turkey... I used sleep() function in my second project, and I included <windows.h>, <time.h> and <iostream>... then, the function worked properly... After a while, my...
19
by: dave.zoltan | last post by:
Hi everybody, I am awfully new to programming in C and all I have had to work with so far have been tutorials that I've found online. In my searching, however, I have not found a solution to a...
0
by: Tim Golden | last post by:
Lowell Alleman wrote: Well you've certainly picked a ticklish area to run into problems with ;). First, forget about the threading aspects for the moment. AFAICT the smallest program which...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.