Skip to content

Instantly share code, notes, and snippets.

@argent-smith
Last active December 11, 2016 10:27
Show Gist options
  • Save argent-smith/f1af13a77bf34f922671a91f68e591cd to your computer and use it in GitHub Desktop.
Save argent-smith/f1af13a77bf34f922671a91f68e591cd to your computer and use it in GitHub Desktop.
open Core_kernel
let setup_logging () =
let open Lwt_log in
default := broadcast [channel ~close_mode:`Keep ~channel:Lwt_io.stderr ()];
add_rule "*" Info
let threaded_function = fun pause ->
Lwt_unix.sleep pause
>> Lwt_log.info_f "Slept for %f" pause
let make_threads () =
let () = Core_random.self_init () in
let num_threads = Core_random.int 10 in
let pauses_generator = Stream.from (fun n -> Some (Core_random.float 5.)) in
let pauses = Stream.npeek num_threads pauses_generator in
Core_list.map ~f:threaded_function pauses
let run_threads () =
let threads = make_threads () in
Lwt_main.run @@ (
Lwt.join threads
>> Lwt_log.info_f "%d threads run" @@ Core_list.length threads
)
let bootup () =
setup_logging ();
run_threads ()
git:(master) ✗ ./main.native
main.native: main: Slept for 0.419942
main.native: main: Slept for 1.676569
main.native: main: Slept for 2.655440
main.native: main: Slept for 3.553326
main.native: main: Slept for 4.232556
main.native: main: Slept for 4.356018
main.native: main: Slept for 4.673952
main.native: main: 7 threads run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment