Does Lwt mean "Light Weight Thread"?

1.1k views Asked by At

I have been testing ocsigen which uses Lwt. I guess Lwt means "Light Weight Thread",right? If so, how can we call it "light weight" ?

It seems Lwt is using OS's thread which is NOT light (compared with Erlang and Haskell).

Please shed a light on me, thanks!

2

There are 2 answers

0
Jeffrey Scofield On BEST ANSWER

The documentation page for Lwt says:

[Lwt] provides very light-weight cooperative threads; ``launching'' a thread is a very fast operation, it does not require a new stack, a new process, or anything else. Moreover context switches are very fast. In fact, it is so easy that we will launch a thread for every system call. And composing cooperative threads will allow us to write highly asynchronous programs.

0
Daniel Bünzli On

Yes, Lwt means lightweight threads and Lwt's threads are light. Lwt has a cooperative threading model, only a single lwt thread runs at the same time on a single system thread, until it explictely yields control. It's your duty to make sure your computation eventually yields otherwise other lwt threads may never run.

However Lwt also has a pool of system threads used to execute blocking system calls or to delegate your long running functions so that they dont't block the whole system and prevent other lwt threads from running.

You can find out more about Lwt's model and implementation in this paper (which you can get on the original author's web page, here).