Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Asynchronous message passing in Objective-C (vallettaventures.com)
17 points by steeleduncan on Jan 8, 2012 | hide | past | favorite | 24 comments


These days, why not use GCD and blocks for this kind of work?


My thoughts exactly.

        NSOperationQueue *queue = [NSOperationQueue mainQueue];
        [queue addOperation:[NSBlockOperation blockOperationWithBlock:^{
            // do something
        }]];
or

        NSOperationQueue *queue = [NSOperationQueue mainQueue];
        [queue addOperation:[[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(selectorName:) object:param1] autorelease]];
Edit: on re-reading the post in detail, I see the problem he is solving is different from the one the above code solves. Clearly there is value to what he has made. I wonder if it could not be re-coded using gcd and/or blocks, but NSOperationQueue is too high-level an API to be appropriate.

Still, I can't tell you how many times I've used the code above to create simple async systems.


How is the problem he is solving different? I guess using an actor chain and messages allows you to repeatedly drop many messages through the same sequence of operations.

While you might express that slightly differently if you used GCD or operation queues, you should be able to make it just as readable. Blocks are pretty cheap, so any performance hit should be more than offset by the gains you get from having the OS manage your thread pool for you.


Also see ActorKit from the inventor of Io: https://github.com/stevedekorte/ActorKit


This reminds me, for some reason even though it's totally different, of NeXT's Distributed Objects facility, which I think is still floating around OS X but am not sure. Here's how it worked.

In Objective-C, method calls are dynamically bound. So you can make the following call:

[myObject crazyMethodName:x:y:z]

In Java that'd be myObject.crazyMethodName(x,y,z)

However unlike Java, there doesn't have to be a method called crazyMethodName, and the compiler will still compile it just fine. What happens is that at runtime, when this method is called, the message "crazyMethodName" is sent to myObject. If it doesn't have a method which responds to that name, then Objective-C calls the "forward" message on myObject, passing in the "crazyMethodName" method name and the arguments. "forward" can do what it likes with this -- the default implementation of "forward" is (was?) to issue a No Such Method Found error.

What Distributed Objects did was create a special proxy object which has a single method implemented: "forward". The system worked as follows. When machine A connects to machine B, machine B gives machine A its "entry" object (as defined by the user). B thinks it gave A an object, but in fact what A received was a proxy object. Let's say that the entry object responds to a method "foo". A calls

[obj foo]

obj is actually a proxy which responds to no methods at all, except for "forward". Objective-C fails to call "foo" on obj, so it tries "forward", and lo and behold the proxy has implemented that in a special way -- it wraps up the method call and ships it over to machine B and calls "foo" on the real object.

Through this procedure objects get passed as arguments back and forth through these method calls, which in turn wind up generating more and more proxies. Ultimately neither machine knows that the objects it's got access to are fakes. It just feels like you've been given access to call and manipulate another processes objects. Very clean and elegant.


No idea why this reminded you of DO -- as you say, it's a totally different thing.

DO still works in OS X. Frankly, it's a neat hack, but it's prohibitively difficult to build anything robust on top of it:

- Error handling is difficult -- what happens when the other side goes down in the middle of your method call? If you have to add extra exception and timeout handling everywhere, that dwarfs the small convenience of making the dispatch code easy.

- Hard to make it secure.

- It's impossible to interoperate with anything else -- what happens when you want to talk to your DO-based server using Java or Windows or Linux?

See "A Note On Distributed Computing" from 1994 for more caveats on this whole approach:

http://labs.oracle.com/techrep/1994/abstract-29.html


I wonder if this inspired DRb[1]. It sounds very similar. The biggest problem is that it's synchronous even across machine boundaries. Celluloid and Dcell[2] are the same basic idea but more explicit.

[1]: http://segment7.net/projects/ruby/drb/introduction.html [2]: http://www.unlimitednovelty.com/2011/05/introducing-celluloi...


That's really cool. For the heck of it I increased the actor count to 16 and CPU-usage stayed ~50% when running (Core i7, dual core w/hyper-threading). If you comment out the NSLog call, it spikes to ~280% CPU usage, as the overhead introduced by NSLog is likely slowing things down.


I greatly prefer PLActorKit by Landon Fuller: http://code.google.com/p/plactorkit/


Why must these sorts of contributions always be BSD licensed, what is wrong with public domain?


The public domain doesn't exist in some places like it does in the US. SQLlite takes a different approach where it is all licensed under the public domain but you can buy a license for $1,000.00


The US too.

The public domain is where things go when their copyright expires. In most countries -- including the United States -- there is no legal procedure to dedicate something to the public domain. You can claim something you wrote is in the public domain until you're blue in the face, but it's not. You still own the copyright on it.

The closest you can get to a public domain declaration is a bare license: basically a license which says "Copyright 2012 by me: I give you the license to do whatever you want with this." The problem with a bare license is that if your code doesn't work right, people can (and do) turn around and sue your pants off. Because -- remember -- the work is not in the public domain. You own it and are responsible for it.

Thus we get to a BSD-style ("academic") license. At a minimum this license says "Copyright 2012 by me: I give you a license to do whatever you want with this as long as you agree not to sue me if something doesn't work".

Always use, at a minimum, a BSD-style license. Public domain is a fiction.


The stridency/accuracy ratio of this comment is interesting.

http://cr.yp.to/publicdomain.html


The GP [SeanLuke] does make one good point, which is that using a BSD-style license is a good idea, because it gives you ammunition to help defeat a "creative" claim that you should be liable for problems other people have with your code.

Otherwise, I agree, there are a fair number of inaccuracies in the GP [SeanLuke] comment.


While conceding immediately that you generally do want to take whatever "free" steps that are available to you to avoid frivolous lawsuits, what's your take on how likely it is that someone would prevail with a claim like "a bug in the software code that you abandoned all claims to cost me millions of dollars"?

(I ask because I'm curious, not to further a debate about PD).


The odds that a plaintiff would prevail in such a claim are close to zero. But defending against the claim would cost money. "Free" steps such as the BSD license give you a better shot at being able to stop the claim at lower cost --- think Spock with a Vulcan neck pinch, as opposed to Kirk having to slug it out out.


Just because Daniel Bernstein claims something on the internet doesn't make it any truer than, well, if I claim it. [okay, maybe a little truer. He did write qmail]

But I'm particularly interested in Bernstein's ad hominem broadside on Rosen for stating, while general counsel for OSI I believe, that public domain dedication is unlikely in the US. Rosen is "only a lawyer" according to Bernstein -- who I might add is "only a mathematician". Though you have to give him props for representing himself in Bernstein v. Commerce.

I myself think that Rosen's conservative position is the right one. Even Lessig's CC0 dedication is couched in tons of careful "we're not sure if this works" verbiage. At any rate, presuming this is unsettled law -- and it seems to me that it's pretty unsettled -- that alone makes it unwise to be attempting a public domain dedication.


Dan Bernstein's a mathematician (albeit one unusually well versed with the legal system, for obvious reasons), but you should also note that one of the commenters on this very subthread is a lawyer.

I'm really not interested in the conflict between the OSI and Dan Bernstein. I'm just responding to your comment.

Pretty sure you're wrong about PD being a "fiction".


> I'm really not interested in the conflict between the OSI and Dan Bernstein. I'm just responding to your comment.

Well, you did directly cite an article, about a third of which is devoted to smacking down someone with more cred than himself.

That being said, my comment about PD being a "fiction" was overly excessive bomb-throwing, I admit.


Thanks.

But, can I just add two things: that's about as specific a legal argument as I've ever seen an engineer (or mathematician) make, including chapter/verse citations to actual court decisions. Not only are you mischaracterizing it, but you might also take a moment to be specific about which parts of it you disagree with. Why should we be interested in how we feel about people's interpersonal conflicts? Bernstein is right or he isn't.

Second: I didn't call you out for bomb-throwing (that would be hypocritical in the extreme). Can I ask, how much of your original comment do you still stand by?


Because Public Domain is complex, the BSD license is pretty clear and applicable in the whole world.


This stackoverflow page has some good comments on what it means to release into public domain: http://stackoverflow.com/questions/219742/open-source-why-no.... The answer, in some cases, is nothing.


> The answer, in some cases, is nothing.

And, in many cases, everything. SQLite users have had this issue in the past: http://www.sqlite.org/copyright.html


It's also convention to license Apple-related projects as they do.

Which are generally MIT/BSD variants.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: