@@ -588,6 +588,40 @@ val s' : int Lwt_stream.t = <abstr>
588
588
Note that a mailbox variable can be seen as a pushable stream with a
589
589
limited memory.
590
590
591
+ == Running a Lwt program ==
592
+
593
+ Threads you create with {{{Lwt }}} always have the type
594
+ {{{Lwt.t }}}. If you want to write a program and run it this is not
595
+ enough. Indeed you don't know when a {{{Lwt }}} thread is terminated.
596
+
597
+ For example if your program is just:
598
+
599
+ <<code language="ocaml"|let _ = Lwt_io.printl "Hello, world!"
600
+ >>
601
+
602
+ you have no guarantee that the thread writing {{{"Hello, world!" }}}
603
+ on the terminal will be terminated when the program exit. In order
604
+ to wait for a thread to terminate, you have to call the function
605
+ {{{Lwt_main.run }}}:
606
+
607
+ <<code language="ocaml"|val Lwt_main.run : 'a Lwt.t -> 'a
608
+ >>
609
+
610
+ This functions wait for the given thread to terminate and returns
611
+ its result. In fact it does more than that; it also run the
612
+ scheduler which is responsible for making thread to progress when
613
+ events are received from the outside world.
614
+
615
+ So basically, when you write a {{{Lwt }}} program you must call at
616
+ the toplevel the function {{{Lwt_main.run }}}. For instance:
617
+
618
+ <<code language="ocaml"|let () = Lwt_main.run (Lwt_io.printl "Hello, world!")
619
+ >>
620
+
621
+ Note that you must call {{{Lwt_main.run }}} only once at a time. It
622
+ cannot be used anywhere to get the result of a thread. It must only
623
+ be used in the entry point of your program.
624
+
591
625
== The {{{lwt.unix }}} library ==
592
626
593
627
The package {{{lwt.unix }}} contains all {{{unix }}} dependant
@@ -610,15 +644,42 @@ val s' : int Lwt_stream.t = <abstr>
610
644
thread. At the end of the program, {{{Lwt }}} will wait for all the
611
645
finaliser to terminates.
612
646
613
- === The lwt scheduler ===
614
-
615
- The {{{Lwt_main }}} contains the {{{Lwt }}} //main loop//. It can be
616
- customized by adding filters, and/or by replacing the {{{select }}}
617
- function.
647
+ === The Lwt scheduler ===
648
+
649
+ Threads doing IO may be put asleep until some events are received by
650
+ the process. For example when you read from a file descriptor, you
651
+ may have to wait for the file descriptor to become readable if no
652
+ data are immediatly available on it.
653
+
654
+ {{{Lwt }}} contains a shceduler which is responsible for managing
655
+ multiple threads waiting for events, and restart them when needed.
656
+ This scheduler is implemented by the two modules {{{Lwt_engine }}}
657
+ and {{{Lwt_main }}}. {{{Lwt_engine }}} is a low-level module, it
658
+ provides signatures for IO multiplexers as well as several builtin
659
+ implementation. {{{Lwt }}} support by default multiplexing IO with
660
+ {{{libev }}} or {{{Unix.select }}}. The signature is given by the
661
+ class {{{Lwt_engine.t }}}.
662
+
663
+ {{{libev }}} is used by default on Unix, because it supports any
664
+ number of file descriptors while Unix.select supports only 1024 at
665
+ most, and is also much more efficient. On Windows {{{Unix.select }}}
666
+ is used because {{{libev }}} does not works properly. The user may
667
+ change at any time the backend in use.
668
+
669
+ The engine can also be used directly in order to integrate other
670
+ libraries with {{{Lwt }}}. For example {{{GTK }}} need to be notified
671
+ when some events are received. If you use {{{Lwt }}} with {{{GTK }}}
672
+ you need to use the {{{Lwt }}} scheduler to monitor {{{GTK }}}
673
+ sources. This is what is done by the {{{lwt.glib }}} package.
674
+
675
+ The {{{Lwt_main }}} module contains the //main loop// of
676
+ {{{Lwt }}}. It is run by calling the function {{{Lwt_main.run }}}:
677
+
678
+ <<code language="ocaml"|val Lwt_main.run : 'a Lwt.t -> 'a
679
+ >>
618
680
619
- Filters are responsible to collect sources to monitor before entering
620
- the blocking {{{select }}}, then to react and wakeup threads waiting
621
- for sources to become ready.
681
+ This function continously run the scheduler until the thread passed
682
+ as argument terminates.
622
683
623
684
=== The logging facility ===
624
685
0 commit comments