Multithreading Magic - Why Everything You Thought You Knew about Concurrency...
slideshare.net
slideshare.net
To some of the comments here...
- Parallel programming may be taught in CompSci courses, for sure, and it's a popular use for 0MQ, but (a) it's not applied to mainstream application development, and (b) it is not designed to scale to networks of any size.
- The Actor model is not key to building a successful message-based concurrent application, but it's a good example, and helps people understand that there are alternatives to shared-state concurrency.
- It's IMO useless to ask people to learn Erlang or Scala to get message based concurrency. People use Java, C, C#, PHP, Python, and probably still COBOL somewhere. So the challenge is how to give this mainstream a toolset that lets them build large parallel applications.
Perhaps next year at FOSDEM we can do a devroom and take the time to see a real application evolve from a simple stand-alone process into a real distributed one. 0MQ is very hard to grasp as a theory, one needs to actually use it for a few days before the beauty a cheap, universal, fast, intelligent, easy to use, and asynchronous queuing messaging fabric hits home.
Cheers!
errno_assert (nbytes != -1);
which translates to #define errno_assert(x) \
do {\
if (unlikely (!(x))) {\
perror (NULL);\
fprintf (stderr, "%s (%s:%d)\n", #x, __FILE__, __LINE__);\
abort ();\
}\
} while (false)
The first thing I noticed is the use of perror() and fprintf() for error reporting. Any daemon that closed its stdin/out/err and not had them redirected to /dev/null will most likely output errors to other file descriptors (sockets, other open files etc.). Is this by design or something that was missed?Secondly, the use of abort() in an API is probably also not the best choice. Say I'm writing a (robust) database server and an error happens on one connection - the error will bring the whole server down, which kind of defeats the purpose. Is this also by design?
[Edit: formatting]
The FOSDEM streaming server seems to be timing out right now.
What I see is that they program something buggy, but where the bugs are rare enough to make the program work most times.
You know how I wrote my first concurrent programs? Create threads with pthread_create(3), and pass ownership of objects by sending pointers over connections created with pipe(3). Which sounds an awful lot like this cure-all actor model (which I hadn't heard of yet) that requires languages almost nobody's ever heard of.
Of all the approaches taken to multithreading, only one is known to work properly. That means, it scales to any number of cores, ...
clone(2) and socket(7)
... avoids all locks,...
Your threads/actors still block waiting for messages. I've seen an example of someone using this to write a mutex in Erlang.
... costs little more than conventional single-threaded programming,...
Probably depends how much you have to twist your code to fit this functional+actor model.
... is easy to learn,...
If you discount having to learn a whole new language and turn your code inside-out.
... and does not crash in strange ways.
Instead of crashing, it hangs because you accidentally wrote a mutex.
This statement suggests to me that you've got some fundamental misunderstandings about the nature of actor based programming. Your actor may block on its message queue (and an infinite computation would mean you'd never consult the queue again), but during a message wait it doesn't necessarily block a thread. That's sort of the point of a good Actor model implementation.
If you write a series of symmetric calls that create a dependency, then yes, you will deadlock; the Actor model can't work its magic when you explicitly model blocking calls.
Perhaps you don't know this, but every lock&block system has an equivalently performant Actor-based system, and vice-versa. (See: http://www.sics.se/~adam/pt/duality78.pdf) What this means, for programmers, is that whichever model fits our current problem best is the one to use. We can make either one performant.
P.S., Just because you haven't heard of these languages doesn't mean they are not widely used. What does your personal experience have to do with the viability of a technique?
A thread can block on a syscall or a "while(true);", but won't necessarily idle one of your CPU cores. That's likewise the point of a good task scheduler.
Actors are threads, just they tend to be implemented in userland rather than as OS threads.
> Perhaps you don't know this, but every lock&block system has an equivalently performant Actor-based system, and vice-versa.
Knowing that they're equivalent (up to "frictionless surface" assumptions for performance, ignoring what primitives the hardware provides) is a significant part of why I don't take claims of the actor model's magicalness seriously.
> P.S., Just because you haven't heard of these languages doesn't mean they are not widely used. What does your personal experience have to do with the viability of a technique?
I have heard of them, always in terms of "this is awesome, what's wrong with people that almost nobody actually uses it?".
What I think of this: Say whaaaaaaaaaaat? Message-passing as a concurrency model? That's crazy talk! Brilliant crazy talk! I wish this was such common knowledge that it was being taught in undergraduate "Parallel Programming" courses everywhere. Oh wait... IT TOTALLY IS!
Speaking of Java here - what's needed is that it's simply not possible to write new Thread(foo).start(); - the message passing model has to be enforced.
I mean, read the Java documentation! What they say about threading is this: "Something slow? Just throw it in a background thread!". It's inane.
How basic is the Actor Model for implementing concurrency using message-passing, besides the obvious advantage of formally defining a common language of terms and proven theorems for what you're doing?
This is far from enlightening.
Enlightenment requires that you change in some way. For me, like many people who have taken the step of downloading and learning 0MQ, the feeling of "wow, this is too easy, where's the catch?" comes only after a few days writing code, and then your brain makes a slight adjustment, and it's all obvious, and that is a small but real enlightenment.
http://en.wikipedia.org/wiki/Communicating_sequential_proces...
There's a patch for Python that does CSP!
http://en.wikipedia.org/wiki/Transputer
http://en.wikipedia.org/wiki/Occam_%28programming_language%2...
Erlang & Scala (for a more object-oriented feel) are both excellent for building actor based systems.
Badly designed you are in the same concurrency hell.
Node.js walks into a bar...