15

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?

Jackson Tale
  • 25,428
  • 34
  • 149
  • 271

2 Answers2

21

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.

Community
  • 1
  • 1
Chuck
  • 234,037
  • 30
  • 302
  • 389
  • 2
    Oh, Node.js is single-threaded? but how can Node.js utilise multiple cpu? – Jackson Tale May 02 '13 at 18:27
  • 5
    @JacksonTale: You run a cluster of Node processes to make full use of multiple CPU cores. – Chuck May 02 '13 at 18:34
  • 1
    ok, understand now. but for that cluster of node processes, each of them occupies a port? and there a load balancer is there? could you please tell me the design of the architecture? – Jackson Tale May 02 '13 at 19:46
  • 3
    @JacksonTale - Rather than pursuing this tangent here in the comments, maybe you should post it as a question. [However, before you do that, read this question and its answers first.](http://stackoverflow.com/questions/2387724/node-js-on-multi-core-machines) – Dave Newman May 02 '13 at 20:12
  • 1
    @ if concurrency diffs from parallelism, then do you think the name of the book `java concurrency in practice` is wrong? – Jackson Tale May 03 '13 at 13:15
  • 1
    If `Concurrency` is used as this meaning, then are all terms talking about `Java Concurrency` actually wrong? – Jackson Tale May 14 '13 at 21:08
  • @JacksonTale: I don't see why that would be, but I don't exactly have a Google alert on "Java concurrency." I'm not sure what you're getting at here. – Chuck May 14 '13 at 21:56
  • I am just thinking that the threading in Java is parallelism, not concurrency according to the link in your answer. right? If I am right, then why everyone is talking about Java Concurrency? – Jackson Tale May 14 '13 at 23:38
  • 1
    @JacksonTale: Ah, I see. They aren't mutually exclusive options. Concurrency means having multiple threads of control at a time. Parallelism means multiple pieces of work are actually being done at the same time. Paralellism in a program generally implies concurrency (spreading work to multiple processors involves having multiple threads of control), though the opposite isn't true (you can have multiple threads but only have one being actively processed at a time). – Chuck May 14 '13 at 23:56
  • To my understanding, you could run a cluster of OCaml processes just like you would with Node.js, but OCaml would need to use async i/o calls in order to get the same type of behavior. Node.js uses a threadpool under the hood combined with utilizing async networking/system calls, but the user's code is running single-threaded in Node.js, which is why people think Node.js is "single-threaded" even though it uses more threads under the hood. In theory OCaml could have the same kind of architecture as Node.js? https://github.com/libuv/libuv – Alexander Mills Dec 21 '18 at 23:25
10

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.

rafix
  • 1,739
  • 9
  • 9