chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] (file-select ...) and compiler warnings


From: Jim Ursetto
Subject: Re: [Chicken-users] (file-select ...) and compiler warnings
Date: Sun, 17 Feb 2013 21:16:56 -0600

On Feb 17, 2013, at 4:18 PM, J Altfas wrote:

> Hello, 
> 
> Using 'file-select' as a sub-second sleep procedure works without hitch under 
> csi: 
> 
> (define (sleeper tm) 
> (let ((currms (current-milliseconds))) 
> (let-values (((_ __) (file-select #f #f tm))) 
> (print (- (current-milliseconds) currms)))))  
> 
> ;1> (sleeper 0.05) 
> 50.0 

Perhaps better is to use thread-sleep!, which does a select or poll behind the 
scenes, but does not block other threads.  file-select will block all threads 
until the timeout occurs.

(use srfi-18)
(define (sleeper tm) 
 (let ((currms (current-milliseconds))) 
  (thread-sleep! tm)
  (print (- (current-milliseconds) currms))))

You can test this by running a thread in the background:

(thread-start! (lambda () (let lp () (print 'ping) (thread-sleep! 2) (lp))))
(sleeper 5.5)

> Of course, having a "real" usleep function would be cleaner than faking it 
> with file-select, especially as implementing a usleep procedure is pretty 
> trivial:             
> 
> (define usleep (foreign-lambda int "usleep" unsigned-integer32)) 

I don't know why sleep(3) was implemented in the posix unit and usleep(3) was 
not.  thread-sleep! is preferable to either, though, in my opinion.

Jim


reply via email to

[Prev in Thread] Current Thread [Next in Thread]