Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am implementing a primitive message-passing system. Currently, I am designing a receive function for async calls. Can I implement such a async receive function without using callback functions?

share|improve this question
    
Does your system need to perform classifications (triage, or sort) of those messages? Is the classification built-into the system, or is it configurable at runtime (requiring registration of "handlers" or "listeners", which are callback objects)? –  rwong Feb 27 '11 at 8:12

5 Answers 5

Received messages can be placed in a queue.

Additionally you can use a signal to indicate to the application when the queue has received a new message.

share|improve this answer
1  
Or the queue can provide a "wait for input" function. This is generally known as concurrent queue. –  rwong Feb 27 '11 at 8:10

I don't think you can, at least not easily. The problem is that while you are waiting for the message you will need to have a thread that is not blocking and that requires a call back.

If you want to do a huge amount of language hackery you might be able to have the async callback be invisible to the programmer but I don't think you can do that in anything that is not lisp. (Or at least I would not want to try!)

share|improve this answer

Depends what language you want to do that with and what communications stack you plan to use.

Anything that handles sockets should be able to do that.

In C at least that's exactly how you are supposed to write a socket server: in a loop that checks or waits for messages, not with callbacks - if your server has other tasks to do, just have another thread handle that.

I would need to know a little bit more about your problem area before I can give any meaningful help.

share|improve this answer

You can replace callbacks with a pointer to a message queue. This not as portable though unless you have wrapper around the message queue facilities to hide the OS dependencies.

share|improve this answer

There's a Scala library for reactive programming which uses Scala's support for delimited continuations to allow the API client to write code as if it were synchronous. I've forgotten the name of the library, unfortunately.

C# 5/VB.NET 11 do the same thing but using a CPS transform in the compiler (similar to the state machine transform they already do for generators; in fact, the two are so similar, that the VB.NET team actually decided to introduce generators in VB.NET 11 as well, since they are able to reuse the design across both features anyway).

StratifiedJS does the same thing for ECMAScript.

Rx is a library for .NET, ECMAScript and soon Objective-C/Cocoa developed by Microsoft, which uses a monadic combinator interface for dealing with asynchronous events.

Functional Reactive Programming is also quite popular.

Because of the rise of distributed computing, this is a very active area of research currently. Continuations, Monads, FRP, Objects, Actors, ... name any program structuring tool you can think of and I bet there is someone working on applying it to asynchronous programming.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.