~ubuntu-branches/debian/sid/ocaml/sid

« back to all changes in this revision

Viewing changes to testsuite/tests/lib-threads/sieve.ml

  • Committer: Bazaar Package Importer
  • Author(s): Stéphane Glondu
  • Date: 2011-04-21 21:35:08 UTC
  • mfrom: (1.1.11 upstream) (12.1.14 sid)
  • Revision ID: james.westby@ubuntu.com-20110421213508-kg34453aqmb0moha
* Fixes related to -output-obj with g++ (in debian/patches):
  - add Declare-primitive-name-table-as-const-char
  - add Avoid-multiple-declarations-in-generated-.c-files-in
  - fix Embed-bytecode-in-C-object-when-using-custom: the closing
    brace for extern "C" { ... } was missing in some cases

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
open Printf
 
2
open Thread
 
3
 
 
4
let rec integers n ch =
 
5
  Event.sync (Event.send ch n);
 
6
  integers (n+1) ch
 
7
 
 
8
let rec sieve n chin chout =
 
9
  let m = Event.sync (Event.receive chin)
 
10
  in if m mod n = 0
 
11
     then sieve n chin chout
 
12
     else Event.sync (Event.send chout m);
 
13
          sieve n chin chout
 
14
 
 
15
let rec print_primes ch max =
 
16
  let n = Event.sync (Event.receive ch)
 
17
  in if n > max
 
18
     then ()
 
19
     else begin
 
20
            printf "%d\n" n; flush stdout;
 
21
            let ch_after_n = Event.new_channel ()
 
22
            in Thread.create (sieve n ch) ch_after_n;
 
23
               print_primes ch_after_n max
 
24
          end
 
25
 
 
26
let go max =
 
27
  let ch = Event.new_channel ()
 
28
  in Thread.create (integers 2) ch;
 
29
     print_primes ch max;;
 
30
 
 
31
let _ = go 1000
 
32
 
 
33
;;