ID:278409
 
Well, i'm currently working on a Project. I've been trying to do sleep() function (Like byond) for a Scripting Database. (It's for a game :P) but i don't get a way to do it.
Like

when sleep() is called, it waits until all the miliseconds passed to continue doing what it was doing (For example, calling another function).

:D?
If you're using Windows, just include windows.h and use its Sleep(int x). It just pauses for x milliseconds.

If you're on a Unix-based system, I seem to recall a sleeping type function that measured in seconds, but I don't recall if it was actually called sleep, and its behavior was somewhat different.

You could always do it the wrong way and use time.h to make your own sleep function. Get the current time, and do while(!time + x milliseconds). That is a stupid sleep though, and it will just lock the application up until it's done sleeping.
In response to Loduwijk
Yes, i've looked into including windows.h and using Sleep() but it pauses the Window.
What i meant was do a custom sleep() (For a Scripting Language/Engine) that pauses the current function until x miliseconds passes.

In response to Ocean King
Ocean King wrote:
What i meant was do a custom sleep() (For a Scripting Language/Engine) that pauses the current function until x miliseconds passes.

That sounds like the same thing, except that you say "the current function." Generally only 1 function is ever being executed at a time, so pausing that 1 function is equivalent to pausing the entire program, and that even includes the Window actions (I'm assuming that, when you say "pauses the Window" you mean that you are doing a GUI app for which the window is unresponsive while it sleeps, as in you can't minimize it, close it, resize, etc.)

If you want multiple functions to be running simultaneously, and that includes executing one function while another handles window actions, then you have to make your application multithreaded, and that is a whole different topic, and it is not nearly as simple a matter. C++ opens a can of worms, but concurrency brings out a whole barrel of worms for which there are several sub-topics you should read up on.

If you are working in a concurrent environment, just call Sleep() in the thread in question.

If your window/GUI response is your only concern, then you should poll to fake concurrency. In your program's logic, you are already supposed to periodically check in on the windows messages and handle them. If you are sleeping for a long period of time, just cut the sleep into shorter periods and handle the event queue during the in-between times.

You can even make your own wrapper for this.
void responsiveSleep(int milliseconds)
{
for(int n = 0; n < milliseconds; n += 100)
{
Sleep(100);
call your window-handler function here
}
}

(edit)
Also, note that the call to your own window handling function is going to add to the time that this sleeps. If you do anything that requires much time (which you shouldn't there), even if only a tenth of a second, that is going to make the responsiveSleep() actually sleep much longer than it is supposed to, potentially twice as long worst-case if your window handling function takes a tenth of a second.
In response to Loduwijk
According to some sources Sleep(int) doesn't work well with values under 100 ms. However in practice it does sleep even with Sleep(1).
In response to Ocean King
Such Sleep() doesn't exist in C++. Any Sleep() in main thread will freeze the window. You need to look into multithreading.

// pseudocode

void func1(){
cout << "Yay I'm a message" << endl;
CreateThread(func2, 100);
}

void func2(int x){
Sleep(x);
cout << "Yay I'm a message 0.1 second later" << endl;
}
In response to Ripiz
I still suggest he stay away from concurrency if he can. If it is just a matter of handling the event queue, it's not necessary.

However, if you choose to go with Ripiz'route and make your application concurrent, you really should read up on the major issues of concurrency. It will save you many headaches later on. Even if you are using C++, you still might benefit from Java's "concurrency trail," a tutorial about the issues you will face. It will describe how to handle the issues in Java, but the basic idea is the same in C++ even though you have to handle everything at a lower level.

Here's a small gift.
http://msdn.microsoft.com/en-us/library/ ms682453%28v=vs.85%29.aspx
In response to Loduwijk
Thanks Loduwijk and Ripiz. ^-^
btw, you may find better answers using a C++-exclusive forum at a website such as http://DaniWeb.com