Jump to content

wait function?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Max.89

Max.89

    Newbie

  • Members
  • Pip
  • 9 posts
I need a function to stop the cpu from doing is work for some seconds.
The seconds passed as argument would be great.
Does something like this exist?

For example:
wait(7); makes the cpu stop for 7 seconds before passing to the next instruction.

#2
roboticforest

roboticforest

    Programmer

  • Members
  • PipPipPipPip
  • 110 posts

Max.89 said:

I need a function to stop the cpu from doing is work for some seconds.
The seconds passed as argument would be great.
Does something like this exist?

For example:
wait(7); makes the cpu stop for 7 seconds before passing to the next instruction.

Are you talking about giving cpu cycles back to the OS? If this is the case, then I suggest that you look for a sleep function, not a wait function.

You could also search Google for things like "sleep function" or "give up cpu cycles", but you might also want to include your OS in the search because such functions are often OS specific.

I haven't looked into this, but I've heard before that you could use the functions found in time.h to create wait functions. The downside is that it's not guaranteed to work, and again it's system dependent.
Dave

#3
whitey6993

whitey6993

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 437 posts
in C++, you can cause the computer to stop using the sleep command included in windows.h.

#include <windows.h>

int main(void)
{
     Sleep(1000);
}

The sleep command has to be capitalized in windows systems and works in milliseconds(ie. 7000 pauses the computer for 7 secs.)

#4
RobotGymnast

RobotGymnast

    Programmer

  • Members
  • PipPipPipPip
  • 143 posts
If you want it to wait for something to happen, you can use events... Try looking up the following functions in MSDN...

Sleep();
SleepEx();
SetEvent();
ResetEvent();
CreateEvent();
OpenEvent();
CloseHandle();

Those should be all you need.

Oh, and if it's time that you're waiting for, use Sleep();