Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: Laure Danthony <ldanthon@ens-lyon.fr>, caml-list@inria.fr
Subject: Re: Unix.sleep et bibliothèque graphique
Date: Fri, 10 Nov 2000 13:47:14 +0100	[thread overview]
Message-ID: <20001110134714.31102@pauillac.inria.fr> (raw)
In-Reply-To: <200011090638.HAA13067@babar.ens-lyon.fr>; from Laure Danthony on Thu, Nov 09, 2000 at 07:38:26AM +0100

[English summary: Laure observed that Unix.sleep returns early when
the graphics window is open.  Under Unix, the graphics library uses
signals for event processing, and on many Unix systems, Unix.sleep
returns early when interrupted by a signal.  Here is an alternate
implementation of sleep that is robust against signals.]

> suite à quelques essais infructeux et de discussions, voici un
> résumé du pb : il s'agit de mettre des pauses au milieu de
> programmes graphiques qui s'éxécutent trop vite
> 1) Le programme suivant [avec Unix.sleep] marche bien.
> 2) Par contre, si j'ajoute open_graph "", cela               
> ne marche plus.                                              

Oui, sur certains systèmes Unix, la fonction Unix.sleep retourne dès
que le processus reçoit un signal, sans attendre la fin du délai
prescrit.  Or, il se trouve que la bibliothèque Graphics utilise des
signaux (pour traiter les événements X de manière asynchrone).

Ce problème n'est pas facile à contourner, il a fallu que je me gratte
la tête un moment, mais voici une réimplémentation de Unix.sleep qui
ne présente pas cet inconvénient:

let mysleep n =
  let start = Unix.gettimeofday() in
  let rec delay t =
    try
      ignore (Unix.select [] [] [] t)
    with Unix.Unix_error(Unix.EINTR, _, _) ->
      let now = Unix.gettimeofday() in
      let remaining = start +. n -. now in
      if remaining > 0.0 then delay remaining in
  delay n

Attention, le paramètre de délai est un flottant, non un entier, ce
qui permet aussi de suspendre le programme pour moins d'une seconde.

Essayez, cela devrait marcher dans un programme utilisant Graphics.

- Xavier Leroy



      parent reply	other threads:[~2000-11-10 18:42 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-11-09  6:38 Laure Danthony
2000-11-10  9:05 ` Sven LUTHER
2000-11-10 12:47 ` Xavier Leroy [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20001110134714.31102@pauillac.inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=ldanthon@ens-lyon.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox