views:

157

answers:

4

Hello, I was wondering if OCaml will perform well in terms of performance and ease of implementation while dealing with typical client/server interactions over TCP in a multi threaded environment.. I mean something really typical like having a thread per client that receives data, operated changes on game states and send them back to clients.

This because I need to write a server for a game and I always did these things in C but since now I know OCaml I was curious to know if it would be ok or I'll just find myself trying to solve a typical problem in a language that doesn't fit well that..

Thanks in advance

+5  A: 

Performance: probably not. OCaml's threads do not provide parallel execution, they are only a way to structure your program. The OCaml runtime itself is not thread-safe, so the only code that could possibly execute in parallel of a single OCaml thread would be interfaced C code (without callbacks to OCaml!).

Implementation-wise, there is a mutex on the run-time, which is released when calling blocking C primitives, and could also be released when calling C functions that do significant work.

Ease of implementation: it wouldn't be world-changing. You would have the comfort of OCaml and a pthread-like library on the side. If you are looking for new things to discover while leveraging what you have learnt of OCaml, I recommend Jocaml. It goes in and out of sync with OCaml, but there was a (re-)re-implementation quite recently, and even when it is slightly out of sync, it is a lot of fun, and a completely new perspective of concurrent programs.

Jocaml is implemented on top of OCaml. What with the run-time not being concurrent and all, I am almost sure it uses separate processes and message-passing. But for the application that you mentioned it should be able to do fine.

Pascal Cuoq
JoCaml provides a (very nice) concurrency model based on message passing to the programmer. Your basic JoCaml program runs in a single Caml runtime; under the hood, the messages are sent between threads of that runtime.JoCaml also provides facilities for distributed programming: messages can be transparently sent between runtimes that don't even need to be running on the same machine. If you use this, there is no special leveraging of the special case where the runtimes happen to be running on the same machine.
Gilles
+1  A: 
Norman Ramsey
+4  A: 

OCaml is quite suitable for writing network servers, although as Pascal observes, there are limitations on threading.

Fortunately, however, threading isn't the only way to organize such a program. The Lwt library (for Light Weight Threads) provides an abstraction of asynchronous I/O that is quite easy to use (particularly when combined with a bit of syntax support). Everything actually runs in one thread, but it's all driven by an asynchronous I/O loop (built on the Unix select call), and the programming style lets you write code that looks like direct code (avoiding much of the normal code overhead of doing asynchronous I/O in many other languages). For example:

lwt my_message = read_message socket in
let repsonse = compute_response my_message in
send_response socket response

Both the read and the write happen back in the main event loop, but you avoid the normal "read, calling this function when you're done" manual overhead.

Michael E
A: 

Why not use a real environment like java or the clr...

mP
I'm just asking how well it would perform, I wrote servers and clients either in Java or C and they look quite similar. I wondered how different it would be on a functional language.. which pros and cons I would have..
Jack
The cons are given that java has a zillion libs for just about everything, my guess eventually you will need one of these...
mP