Skip to content

Instantly share code, notes, and snippets.

@philtomson
Created July 9, 2014 21:44
Show Gist options
  • Save philtomson/61b72bdc271de1a69e00 to your computer and use it in GitHub Desktop.
Save philtomson/61b72bdc271de1a69e00 to your computer and use it in GitHub Desktop.
A simple producer-consumer concurrency test using Core.Async
(* compile with:
corebuild -pkg async,unix test_async.native
*)
open Sys
open Async.Std
open Core
let (r,w) = Pipe.create ()
(* producer *)
let countup hi w =
let rec loop i =
printf "i=%d\n" i ;
if (i < hi &&( not (Pipe.is_closed w))) then
Pipe.write w i >>>
fun () -> loop (i+1)
else Pipe.close w
in
loop 0 ;;
(* consumer *)
let rec readloop r =
Pipe.read r >>=
function
| `Eof -> return ()
| `Ok v -> return (printf "Got %d\n" v) >>=
fun () -> after (Time.Span.of_sec 0.5) >>=
fun () -> readloop r ;;
let () =
Pipe.set_size_budget r 256 ;
countup 10 w;
ignore(readloop r);
Core.Never_returns.never_returns (Scheduler.go ())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment