Why OCaml's threading is considered as `not enough`?

It seems many people are saying OCaml does not have a good capacity for concurrency and it is also not good for web server applications.

I am currently learning ocaml's manual. It seems that OCaml provide concurrency now.

Can I know why OCaml's concurrency/threading is considered as bad?

Can I develop server application in OCaml? What problems may I meet?


See Concurrency vs. parallelism — What's the difference?. OCaml's threads offer concurrency, as you can have the next function start before the previous one is finished. But OCaml does not offer parallelism, so when that second function starts, the first one has to be put on hold. Two threads cannot run simultaneously, so multiple CPU-bound computations in a process will block each other, and you can't max out all your CPU cores in one process.

That's people's beef with OCaml's threading. Does it mean you can't use OCaml for something like a server? No. It's something you will have to take into account in your server design, but it's not generally a showstopper. Heck, Node.js is straight-up single-threaded, yet its main purpose is creating servers.


OCaml supports the usage of multiple threads. But only one ocaml thread can run at a given point in time, there is never a parellelism of different ocaml threads.

However:

  • you can fork / use multiple processes.

  • external code (eg. external c/c++-libraries) can run in parallel, as long as their interaction with the ocaml runtime is properly controlled.

  • PS: The linked document is not the ocaml manual. It's a good, but dated book about OCaml.

    Appendix: Of course, you can develop servers in ocaml (live example: ocsigen - a web server ). It depends on your needs, if the lack of real thread concurreny is a feature or disadvantage.

    链接地址: http://www.djcxy.com/p/13930.html

    上一篇: Java线程和内核数量

    下一篇: 为什么OCaml的线程被认为是“不够”?