views:

533

answers:

6

Hi,

a theoretical question. After reading Armstrongs 'programming erlang' book I was wondering the following: It will take some time to learn Erlang. Let alone master it. It really is fundamentally different in a lot of respects.

So my question: Is it possible to write 'like erlang' or with some 'erlang like framework', which given that you take care not to create functions with sideffects, you can create scaleable reliable apps as well as in Erlang? Maybe with the same msgs sending, loads of 'mini processes' paradigm.

The advantage would be to not throw all your accumulated C/C++ knowledge over the fence.

Any thoughts about this would be welcome

+2  A: 

I would break this into 2 questions

Can you write concurrent, scalable C++ applications

Yes. It's certainly possible to create the low level constructs needed in order to achieve this.

Would you want to write concurrent, scalable, C++ applications

Perhaps. But if I was going for a highly concurrent application, I would choose a language that was either designed to fill that void or easily lent itself to doing so (Erlang, F# and possibly C#).

C++ was not designed to build highly concurrent applications. But it can certainly be tweaked into doing so. The cost might be higher than you expect though once you factor in memory management.

JaredPar
Why would you prefer C# over C++? In C++ you can do things like align shared data structures on cache lines, for example.
RussellH
@RusselH, My 2 primary reasons would be memory management and better threading support. C++ has threading support but it's in many ways an afterthought. C# has designed htreading support (although it lacks in many areas).
JaredPar
I like c#, but I don't see why it makes creating concurrent scalable apps easier. If anything, it hides a lot of details which might be very relevant.
Toad
+1  A: 

Yes, but you will be doing some extra work.

Regarding side effects, consider how the .net/plinq team is approaching. Plinq won't be able to enforce you hand it stuff with no side effects, but it will assume you do so and play by its rules so we get to use a simpler api. Even if the language doesn't have built-in support for it, it will still simplify things as you can break the operations more easily.

eglasius
A: 

the main 'problem' with C (or C++) for writing reliable and easy to extend programs is that in C you can do anything. so, the first step would be to write a simple framework that restricts just a bit. most good programmers do that anyway.

in this case, the restrictions would be mostly to make it easy to define a 'process' within whatever level of isolation you want. fork() has a reputation of being slow, and threads also need significant time to spawn, so you might want to use a cooperative multitasking, which can be far more efficient, and you could even make it preemptive (i think that's what Erlang does). to get multi-core efficiency, set a pool of threads and make all of them complete to run the tasks.

another important part would be to create an appropriate library of immutable data structures, so that using them (instead of the standard lib) your functions would be (mostly) side-effect-free.

then it's just a matter of setting a good API for message passing and futures... not easy, but at least it doesn't seem like changing the language itself.

Javier
great! But does it exist already? ;^)
Toad
+1  A: 

What I can do in one Turing complete language I can do in any other Turing complete language.

So I interpret your question to read, is it as easy to write a reliable and scalable application in C++ as it is in Erlang?

The answer to that is highly subjective. For me it is easier to write it in C++ for the following reasons:

  • I have already done it in C++ (at least three times).
  • I don't know Erlang.
  • I have read a great deal about Stackless Python, which feels to me like a highly concurrent message based cooperative multitasking system in python, but of course python is written on top of C.

Having said that. If you already know both languages, and you have the problem well defined, you can then make the best choice based on all the information you have at hand.

grieve
FYI: The canonical Python interpreter, CPython, is written in C, not C++.
mipadi
@mipadi: Good point. Edited my post to fix it.
grieve
did you use a framework for your c apps? Or did you also 'reinvent the wheel'as is suggested in previous replies here?
Toad
@reinier: Both really. We wrote the framework, then built on top of that. We were developing a framework/application that had to work across various platforms (long before boost), so in some cases we had no choice. Things are a bit easier now. :)
grieve
stackless Python uses 'continuation passing style' (CPS) to get highly flexible codepaths, (that's why it's stackless). on that writing style, getting concurrency is almost trivial. the hard part is getting used to it.
Javier
+7  A: 

Yes, it is possible, but...

Probably the best answer for this question is given by Robert Virding’s First Rule:

“Any sufficiently complicated concurrent program in another language contains an ad hoc, informally-specified, bug-ridden, slow implementation of half of Erlang.”

Very good rule is use the right tool for the task. Erlang excels in concurrency and reliability. C/C++ was not designed with these properties in mind.

If you don't want to throw away your C/C++ knowledge and experience and your project allows this kind of division, good approach is to create a mixed solution. Write concurrent, communication and error handling code in Erlang, then add C/C++ parts, which will do CPU and IO bound stuff.

gleber
AFAIK, the original quote was about LISP. of course, anybody would say it's more appropriate about his favorite language of the day, but the minimalistic approach of (original) LISP makes it a little 'truer'
Javier
Didn't knew it was about Lisp. Thanks, Javier
gleber
+3  A: 

You clearly can - the Erlang/OTP system is largely written in C (and Erlang). The question is 'why would you want to?'

In 'ye olde days' people used to write their own operating system - but why would you want to?

If you elect to use an operating system your unwritten software has certain properties - it can persist to hard disk, it can speak to a network, it can draw on screens, it can run from the command line, it can be invoked in batch mode, etc, etc...

The Erlang/OTP system is 1.5M lines of code which has been demonstrated to give 99.9999999% uptime in large systems (the UK phone system) - that's 31ms downtime a year.

With Erlang/OTP your unwritten software has high reliability, it can hot-swap itself, your unwritten application can failover when a physical computer dies.

Why would you want to rewrite that functionality?

Gordon Guthrie
31ms a year? I wonder how they measured that :)
which is great... but why does it have to be a completely new language. It would be great if either:a) there would be a more c syntaxy like language which could be run in the erlang VMorb) there would be an erlang framework which you can hook up to cRecursions are great but also good mindf***s
Toad
It isn't a completely new language - it is the 14th Annual user conference this year... It is a successful and widely used telecomms language that has spread to internet business because it address the problems of reliability and scalability well.
Gordon Guthrie
true... I meant as seen from the eyes of an imperative-language programmer. Thanks for the insight in any case.
Toad
The functional programming part of Erlang/OTP is the least important part of it in respect to this...You can hook C in as well with a variety of mechanism see the interoperability tutorial (http://erlang.org/doc/tutorial/part_frame.html)
Gordon Guthrie