SRL Archive - Jr. Member General Help and Discussion

Wait and Sleep


Lucidity

2015-06-17 07:43:15

Is there a difference between the functions :
wait(100);
and
sleep(100);

If so, what are the differences?

srlMW

2015-06-17 08:27:33

I'm now actually also interested in the answer to this!

Citrus

2015-06-17 10:11:04

I've wondered about this too. Maybe someone can actually explain it but here is what a quick search turned up
1.“Sleep” is a thread management method that is defined as a static method while “wait” is a thread management method which is defined in the Object class.
2.Sleep is a method that executes threads while wait is a method that executes objects.
3.Sleep keeps the synchronization locks of the current thread and influences the behavior of applications while wait does not.
4.Sleep controls the execution of the thread and does not allow other threads in a synchronized block while wait does not control the execution of the thread to let other threads run.


source:
http://www.differencebetween.net/technology/software-technology/difference-between-sleep-and-wait/

Clarity

2015-06-17 11:55:47

From: evilcitrus
…


Seems more relevant to multithreading then, which scripts don't support yet, unless this means the script running thread versus for example the extensions, and other stuff going on in Simba. A dev answer would be awesome, I'm curious too.

Lucidity

2015-06-17 12:08:30

Well I went searching on the GIT.. this is what I found
procedure Sleep(milliseconds: Cardinal);
begin
{$IFDEF MSWINDOWS}
{$IFDEF FPC}
sysutils.sleep(milliseconds);
{$ELSE}
windows.sleep(milliseconds);
{$ENDIF}
{$ELSE}
sysutils.sleep(milliseconds);
{$ENDIF}

end;

https://github.com/MerlijnWajer/Simba/blob/1327b37c83a99d14279c25fd610b134bbcadba39/Units/Synapse/synafpc.pas

Olly

2015-06-17 13:15:05

It's exactly the same… Wait just calls sleep iirc. If you want to know why its there ask who added Wait to scar.

Turpinator

2015-06-17 14:15:33

From: Olly
It's exactly the same… Wait just calls sleep iirc. If you want to know why its there ask who added Wait to scar.


yup.

procedure ps_Wait(t: DWord); extdecl;
{$ifdef MSWINDOWS}
begin
Sleep(t);
end;
{$else}
var
EndTime : DWord;
begin
if t > 50 then
begin;
EndTime := GetTickCount + t;
while {(CurrThread.PSScript.Exec.Status = isRunning) and }(GetTickCount < EndTime) do
Sleep(16);
end else
begin
sleep(t);
end;
end;
{$endif}
https://github.com/MerlijnWajer/Simba/blob/1327b37c83a99d14279c25fd610b134bbcadba39/Units/MMLAddon/PSInc/Wrappers/other.inc#L40

So we are clear, lape_Wait() calls ps_Wait() (the proc above).

Le Jingle

2015-06-17 14:48:41

it's native (sleep) and the clock cycle ms is important in sleep.
so always use wait, and not have the added consequence in sleep, but basically have all the benefits of sleep.

masterBB

2015-06-17 18:13:13

From: Olly
It's exactly the same… Wait just calls sleep iirc. If you want to know why its there ask who added Wait to scar.


Probably because the name sleep refers to setting a thread in sleep mode. Most starting script writers don't know what a thread is, thus the name wait fits that function way better. But that is just an opinion of course.

bonsai

2015-06-18 04:01:33

From: Turpinator
yup.

procedure ps_Wait(t: DWord); extdecl;
{$ifdef MSWINDOWS}
begin
Sleep(t);
end;
{$else}
var
EndTime : DWord;
begin
if t > 50 then
begin;
EndTime := GetTickCount + t;
while {(CurrThread.PSScript.Exec.Status = isRunning) and }(GetTickCount < EndTime) do
Sleep(16);
end else
begin
sleep(t);
end;
end;
{$endif}
https://github.com/MerlijnWajer/Simba/blob/1327b37c83a99d14279c25fd610b134bbcadba39/Units/MMLAddon/PSInc/Wrappers/other.inc#L40

So we are clear, lape_Wait() calls ps_Wait() (the proc above).


Looks like it was written to catch script/thread exits on linux.

Since that was commented, it's pointless and wasting cpu.