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

馋奶兔 提交于 2019-11-30 08:32:25
Chuck

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!