<p>Actually, this question seems to have two parts:</p>

<ul>
<li>How to implement pattern matching?</li>
<li>How to implement <a href="http://erlang.org/doc/reference_manual/expressions.html#6.9" rel="noreferrer">send and receive</a> (i.e. the Actor model)?</li>
</ul>

<p>For the pattern matching part, I've been looking into various projects like <a href="http://members.cox.net/nelan/app.html" rel="noreferrer">App</a> and <a href="http://www.cs.nyu.edu/leunga/papers/research/prop/prop.html" rel="noreferrer">Prop</a>.  These look pretty nice, but couldn't get them to work on a recent version (4.x) of g++.  The <a href="http://felix-lang.org/" rel="noreferrer">Felix</a> language also seems to support pattern matching pretty well, but isn't really C++.</p>

<p>As for the <a href="http://en.wikipedia.org/wiki/Actor_model" rel="noreferrer">Actor model</a>, there are existing implementations like ACT++ and <a href="http://theron.ashtonmason.net/" rel="noreferrer">Theron</a>, but I couldn't find anything but papers on the former<strike>, and the latter is single-threaded only</strike> [see answers].</p>

<p>Personally, I've implemented actors using threading and a thread-safe message queue.  Messages are hash-like structures, and used these together with a number of preprocessor macros to implemented simple pattern matching.</p>

<p>Right now, I can use the following code to send a message:</p>

<pre><code>(new Message(this))
    ->set("foo", "bar")
    ->set("baz", 123)
    ->send(recipient);
</code></pre>

<p>And the following to do simple pattern matching (<code>qDebug</code> and <code>qPrintable</code> are Qt-specific):</p>

<pre><code>receive_and_match(m)
    match_key("foo")    { qDebug("foo: %s", qPrintable(m->value("foo").toString())); }
    or_match_key("baz") { qDebug("baz: %d", m->value("baz").toInt()); }
    or_match_ignore
end_receive
</code></pre>

<p>However, this looks a bit hackish to me, and isn't very robust.</p>

<p>How would you do it?  Did I miss any existing work?</p>

[{'answer_id': 40616, 'author': '1729', 'author_id': 4319, 'author_profile': 'https://Stackoverflow.com/users/4319', 'pm_score': 3, 'selected': True, 'text': "<p>One of the important things about erlang is how the features are used to make robust systems.</p>\n\n<p>The send/recieve model is no-sharing, and explicitly copying.\nThe processes themselves are lightweight threads.</p>\n\n<p>If you did desire the robust properties of the erlang model, you would be best to use real processes and IPC rather than threads.</p>\n\n<p>If you want robust message passing though you may end up wanting to serialize and deserialise the contents. Especially with type safety.</p>\n\n<p>Pattern matching in C++ isn't always pretty but there will be a good pattern for this - you will end up creating a dispatcher object that uses some form of polymorphism to get what you want.</p>\n\n<p>Although if you are not careful you end up with xml over pipes :)</p>\n\n<p>Really, if you want the erlang model you really want to use erlang. If there are slow bits, I'm sure you can augment your program using a foreign function internet.</p>\n\n<p>The problem about re-implementing parts, is you won't get a good cohesive library and solution. The solutions you have already don't look much like C++ anymore.</p>\n"}, {'answer_id': 185138, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': "<blockquote>\n  <p>As for the Actor model, there are\n  existing implementations like ACT++\n  and Theron, but I couldn't find\n  anything but papers on the former, and\n  the latter is single-threaded only.</p>\n</blockquote>\n\n<p>As the author of Theron, I was curious why you believe it's single-threaded?</p>\n\n<blockquote>\n  <p>Personally, I've implemented actors\n  using threading and a thread-safe\n  message queue</p>\n</blockquote>\n\n<p>That's how Theron is implemented.. :-)</p>\n\n<p>Ash</p>\n"}, {'answer_id': 912489, 'author': 'neverlord', 'author_id': 112736, 'author_profile': 'https://Stackoverflow.com/users/112736', 'pm_score': 2, 'selected': False, 'text': '<p>I\'m currently implementing an actor library for C++ called "acedia" (there\'s nothing yet about it on google) that uses "type matching". The library is a project for my master thesis and you can send any kind of data to an actor with it.</p>\n\n<p>A small snippet:</p>\n\n<pre><code>recipient.send(23, 12.23f);\n</code></pre>\n\n<p>And on the recipient side you can either analyze the received message like this:</p>\n\n<pre><code>Message msg = receive();\nif (msg.match&lt;int, float&gt;() { ... }\n</code></pre>\n\n<p>... or you can define a rule set that invokes a function or method for you:</p>\n\n<pre><code>void doSomething(int, float);\n\nInvokeRuleSet irs;\nirs.add(on&lt;int, float&gt;() &gt;&gt; doSomething);\nreceiveAndInvoke(irs);\n</code></pre>\n\n<p>It\'s also possible to match both on type and on value:</p>\n\n<pre><code>Message msg = receive();\nif (msg.match&lt;int, float&gt;(42, WILDCARD) { ... }\nelse if (msg.match&lt;int, float&gt;() { ... }\n</code></pre>\n\n<p>The constant "WILDCARD" means, that any value will be acceptet. Pass no arguments is equal set all arguments to "WILDCARD"; meaning that you only want to match the types.</p>\n\n<p>This is certainly a small snippet. Also you can use "case classes" like in Scala. They are comparable to "atomics" in erlang. Here is a more detailed example:</p>\n\n<pre><code>ACEDIA_DECLARE_CASE_CLASS(ShutdownMessage)\nACEDIA_DECLARE_CASE_CLASS(Event1)\nACEDIA_DECLARE_CASE_CLASS(Event2)\n</code></pre>\n\n<p>To react to the defined case classes you can write an actor like this:</p>\n\n<pre><code>class SomeActor : public Actor\n{\n\n  void shutdown() { done = true; }\n  void handleEvent1();\n  void handleEvent1();\n\n  public:\n\n    SomeActor() : done(false) { }\n\n    virtual void act()\n    {\n      InvokeRuleSet irs;\n      irs\n        .add(on&lt;ShutdownMessage&gt;() &gt;&gt; method(&amp;SomeActor::shutdown))\n        .add(on&lt;Event1&gt;() &gt;&gt; method(&amp;SomeActor::handleEvent1))\n        .add(on&lt;Event2&gt;() &gt;&gt; method(&amp;SomeActor::handleEvent2))\n      ;\n      while (!done) receiveAndInvoke(irs);\n    }\n\n};\n</code></pre>\n\n<p>To create a new actor and start it, all you have to write is:</p>\n\n<pre><code>Acedia::spawn&lt;SomeActor&gt;();\n</code></pre>\n\n<p>Although the library not even reached beta stadium the shown snippets work and i have a first application running on it. One major goal of the library is to support distributed programming (also across a network).</p>\n\n<p>Your question is a while ago, but if you\'re interested in it: let me know! :)</p>\n'}, {'answer_id': 929028, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>I would definitely be interested in looking at your "acedia" library and would love to help in any way that I could. Erlang has some wonderful constructs and C++ could definitely benefit from such a library.</p>\n'}, {'answer_id': 966716, 'author': 'neverlord', 'author_id': 112736, 'author_profile': 'https://Stackoverflow.com/users/112736', 'pm_score': 0, 'selected': False, 'text': '<p>Today I hostet the library at sourceforge: <a href="https://sourceforge.net/projects/acedia/" rel="nofollow noreferrer">https://sourceforge.net/projects/acedia/</a></p>\n\n<p>As I said before it\'s an early release. But feel free to critique it!</p>\n'}, {'answer_id': 4140159, 'author': 'sivabudh', 'author_id': 65313, 'author_profile': 'https://Stackoverflow.com/users/65313', 'pm_score': 2, 'selected': False, 'text': "<p>You can mimic the behavior using Qt's signal/slot mechanism, especially since Qt's signal/slot supports multithread.</p>\n"}, {'answer_id': 23127725, 'author': 'centaurian_slug', 'author_id': 1044688, 'author_profile': 'https://Stackoverflow.com/users/1044688', 'pm_score': 0, 'selected': False, 'text': '<p>Today, if you want erlang style robust actors in C++, and pattern matching,\nmaybe Rust is the answer. </p>\n\n<p>Of course this wasn\'t around publically when the OP asked ~5years ago, and as of april 2014 it still isn\'t v1.0 yet - but its been progressing very well and is definitely stabilizing, enough of the language core is stable I think.</p>\n\n<p>And ok its not C++, but it has the same approach to memory management as C++, except that it supports lightweight tasks with no shared memory by default (then provides controlled library features for sharing - "Arc"); \nIt can directly call (and directly expose) \'extern C\' functions. You can\'t share templated library headers with C++ - but you can write generics that mimick C++ collection classes (and vica versa) to pass references to data-structures across.</p>\n'}]
['https://Stackoverflow.com/questions/40423', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']