Both C # and Scala adopted a framework to simplify asynchronous / parallel computing, but in different ways. The latest version of C # (5.0, still in beta) decided to use the async / wait structure (using a continuation that goes under the hood, but is easier to use), while Scala instead uses the concept of “actors” and more recently accepted actors in Akka and included them in the base library.
Here's the problem: We get a series of requests to perform various operations - for example, from user input, server requests, etc. Some operations are quick, but some take some time. For slow ones, we would like to perform an operation asynchronously (in another thread) and process it when the thread is running, but at the same time are free to process new requests.
A simple synchronous loop can be (pseudo-code):
while (1) { val request = waitForAnything(user_request, server_request) val result = do_request(request) if (result needs to be sent back) send_back_result(result) }
In the base fork / join structure, you can do something like this (pseudocode):
val threads: Set[Thread] while (1) { val request = waitForAnything(user_request, server_request, termination of thread) if (request is thread_terminate) { threads.delete(request.terminated_thread) val result = request.thread_result if (result needs to be sent back) send_back_result(result) } else if (request is slow) { val thread = new Thread(() => do_request(request)) Threads.add(thread) thread.start() } else { val result = do_request(request) if (result needs to be sent back) send_back_result(result) } }
What would it look like using asynchronous / waiting and using participants, and in general, what are the advantages / disadvantages of this approach?
source share