Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 394
Jackson Tale Avatar asked May 02 '13 16:05

Jackson Tale


People also ask

Under what conditions multithreading is not possible and why?

Many tasks may be compute bound, but still not practical to use a multithreaded approach because the process must synchronise on the entire state. Such a program cannot benefit from multithreading because no work can be performed concurrently.

What are problems with threads?

When using threads, it can cause increased complexity, and debugging your code can become much more difficult. It is possible to add logic to make sure data is synchronized across threads, but too much reliance on synchronization can lead to performance issues, which affects an application's scalability.

Is OCaml single threaded?

Only one thread at a time is allowed to run OCaml code, hence opportunities for parallelism are limited to the parts of the program that run system or C library code. However, threads provide concurrency and can be used to structure programs as several communicating processes.

Do threads actually run in parallel?

On a single core microprocessor (uP), it is possible to run multiple threads, but not in parallel. Although conceptually the threads are often said to run at the same time, they are actually running consecutively in time slices allocated and controlled by the operating system.


1 Answers

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.

like image 147
Chuck Avatar answered Oct 31 '22 23:10

Chuck