Recent

Author Topic: sleep alternative  (Read 960 times)

toby

  • Sr. Member
  • ****
  • Posts: 282
Re: sleep alternative
« Reply #15 on: July 03, 2026, 10:59:25 pm »

Fibonacci your post #1

i added   uses cthreads    to the program or else the function wouldn't even wait 2.5 seconds
but the funtion seems to still block the  writeln('waited 2.5 secs');  line
if the nosleep wasn't blocking it would print out '11' after the '22' and not before

Code: Pascal  [Select][+][-]
  1.  
  2. program timer;
  3. uses cthreads;
  4.  
  5. function nosleep(timeout: integer): PRTLEvent;
  6. begin
  7. result := RTLEventCreate;
  8. RTLEventWaitFor(result, timeout);
  9. RTLEventDestroy(result);
  10. writeln('11');
  11. end;
  12.  
  13. begin
  14. nosleep(2500);
  15. writeln('22');
  16. end.
  17.  
  18.  



thaddy

your post #4 does not compile with error
a:=TThread.ExecuteInThread(procedure
Syntax error, ")" expected but "BEGIN" found

the sleep procedure one compiles after adding missing integers and doesn't block as i needed

and the next one doesn't compile like the one above the sleepy procedure
a:=TThread.ExecuteInThread(procedure
Syntax error, ")" expected but "BEGIN" found

Thaddy

  • Hero Member
  • *****
  • Posts: 19433
  • Glad to be alive.
Re: sleep alternative
« Reply #16 on: July 04, 2026, 01:36:27 pm »
That is normal: anonymous functions are only available in fpc trunk 3.3.1. You can adapt it according to my example 2, with a function and a function pointer. This works from 3.2.2:
Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2. uses {$ifdef unix}cthreads,{$endif}sysutils,classes;
  3.  
  4. procedure Sleepy(Data:Pointer);
  5. begin
  6.   sleep(2500);
  7.   writeln('slept for 2,5 seconds');
  8. end;
  9.  
  10. var
  11.   a:TThread;
  12.   i:integer = 0;
  13.   s:integer = 2500; // sleep value
  14. begin
  15.   // non-blocking sleep
  16.   a:= TThread.ExecuteInThread(@Sleepy);
  17.   // main workload                            
  18.   repeat inc(i);until a.finished;
  19.   writeln('cycles in main: ',i);
  20. end.
« Last Edit: July 04, 2026, 02:26:23 pm by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

Warfley

  • Hero Member
  • *****
  • Posts: 2071
Re: sleep alternative
« Reply #17 on: July 04, 2026, 02:23:19 pm »
i added   uses cthreads    to the program or else the function wouldn't even wait 2.5 seconds
but the funtion seems to still block the  writeln('waited 2.5 secs');  line
if the nosleep wasn't blocking it would print out '11' after the '22' and not before

Code: Pascal  [Select][+][-]
  1.  
  2. program timer;
  3. uses cthreads;
  4.  
  5. function nosleep(timeout: integer): PRTLEvent;
  6. begin
  7. result := RTLEventCreate;
  8. RTLEventWaitFor(result, timeout);
  9. RTLEventDestroy(result);
  10. writeln('11');
  11. end;
  12.  
  13. begin
  14. nosleep(2500);
  15. writeln('22');
  16. end.
  17.  
  18.  
Again, what do you expect your sleep alternative to do? Computers are stupid, they just execute one instruction after another. So any function like sleep, would need to be able to tell the computer what to do during that time.
How does the computer know what functions require the waiting and what don't? Assume there is a non blocking wait function called nbwait, what should happen in the following example:
Code: Pascal  [Select][+][-]
  1. procedure Test;
  2. begin
  3.   nbwait(1000);
  4.   WriteLn('a');
  5.   WriteLn('b');
  6. end;
  7.  
  8. begin
  9.   Test;
  10.   WriteLn('c');
  11. end.
Here are 3 equally valid options:
1. nbWait does literally nothing and a, b and c are printed right after each other in order
2. nbwait delays the next statement, so the writing of a, so b and c are written immediately and a is waited for
3. nbwait delays the current function and c is written but a and b are waited for

The first would probably be useless. The second is not possible in pascal, this would require some callback. The third option would require nbwait to know the context of the function and also if that function produces a result, the calling function can't know when it's finished.

Such an nbwait function simply cannot exist. Either you use threads, or you use something like async await.

If you want to do async-await style programming, you could try out the aforementioned STAX. Your example would be:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   SysUtils,stax,stax.functional;
  7.  
  8.  
  9. procedure NoSleep(AExecutor: TExecutor);
  10. begin
  11.   AsyncSleep(2500);
  12.   WriteLn('11');
  13. end;
  14.  
  15. procedure Test(AExecutor: TExecutor);
  16. begin
  17.   RunAsync(AsyncProcedure(@NoSleep));
  18.   WriteLn('22');
  19. end;
  20.  
  21. var
  22.   exec: TExecutor;
  23. begin
  24.   exec := TExecutor.Create;
  25.   exec.RunAsync(AsyncProcedure(@Test));
  26.   exec.Run;
  27.   exec.Free;
  28.   ReadLn;
  29. end.

But, it's kindof a hack. Instead you should probably reconsider either using threads, or using a different language that has async-await supported on the language level
« Last Edit: July 04, 2026, 02:38:00 pm by Warfley »

 

TinyPortal © 2005-2018