I'm fairly new to advanced C++ program techniques such as templates,
but I am developing a simple API for a project I'm working on.
The function or method that you call can take a long time to complete.
Essentially it's transferring a file over the network.
It looks a bit like this.
Client
{
int WriteFile();
int ReadFile();
}
But I want to have a couple of options here.
call WriteFile and have it block.
Call WriteFileAsync and not have it block.
In the async version be flexible about how I know the task is done.
Be able to poll the client to find out where it's up to with my current Read or Write
operation.
I'm at a bit of a loss as to how to design this nicely the C++ way.
It's a requirement to avoid using boost, but I could use a boost-like approach.
Although, I looked through some of the headers and got very much confused. Anything beyond
basic template programming for me I find confusing.
What I'm after is a nice way of being notified of event completion and be able to wait for
an event to complete.
My advice would be looking at the docs and tutorial for boost::asio (which you can use as part of boost or as part of the independent asio project, but I guess that the requirement is no external libs, not just no boost).
Usually blocking calls are simple to define, while non-blocking operations require some callback mechanism as to notify the user of the result of the operation whenever that completes. Again, take a look at the tutorials and docs to get an idea of a clean interface, that will be much easier to browse over than the headers.
EDIT: ASIO has support for different protocols, so it might be more complex than what you need, read one of the examples and get the ideas of how to use callback mechanisms.
Regarding the use of asynchronous calls, I would suggest reading about the design of the future for C++0x.
Basically, the idea is to hand a proxy to the user, instead of a plain type. This proxy is aware of the threading and can be used to:
poll about the completion
get the result
You can also add clever mechanisms like trying to get the result for a fixed duration or up to a fixed point in time and abandon (for the moment) if the task hasn't completed in time (for example to do something else and try again later, or to simple go forward and forget about this).
The new threading API of C++0x has been very cleverly designed (taking mainly after Boost.Threads) and would give you much insight as to how to design for multi-threading.
Related
Akka seems like a dream come true. Sadly, like so much other software, the documentation and examples are lacking in some major ways. Since the whole point of the thing is to provide non-blocking, parallel io, why would they provide a hello world that just returns a string. Here's a nutty idea: have an agent for each word, translate it into another language by calling something on the web, then returning the results.
I went around in circles today reading documentation about Futures and Promises. One working example would have obviated the whole thing.
I have done a lot of concurrent programming with Future in the java concurrency package. For some reason, the Akka stuff just seems way too complicated. I am doing something very close to what I described above: getting a request and having several agents fulfill it over the web. I took the original generated project that has the Master and the Listener as the starting point and it works fine, I just can't figure out a simple way to return the aggregated results. I have a play-mini method that is getting called. From there, I am calling a method on a class that sends the messages to the agents and when they are done running, their results get aggregated and the Listener gets called. How do I compose a Future out of that? All the documentation says don't block but we are having to return from a REST request.
Does anyone know of such an example? Super simple. Thanks.
I ended up doing composed Futures. Works pretty well. When you create a sequence, you still have to call Await, but the parallel execution still returned in ⅓ of a second so I'm happy.
As to getting Actors to handle a REST request, I thought about passing it a Future and then waiting on that? Might play around with some of those possibilities, but what I have now works.
The other question this experience raised for me is how to implement Ask in an Actor. Not covered in the docs and given the name, searching for Akka and ask is pretty much useless.
Here's a suggestion: each of these mechanisms should be shown in sequence diagrams. How hard would that be to do??
Still really excited about Akka. It's awesome to finally be able to do Actor-based programming.
I have a homework task that requires me to learn how to use the Guava concurrency library.
In the task I have several thread pools, where each one is controlled by an individual object.
Each pool has several working threads that perform simple tasks (mostly emulating doing stuff by using Thread.sleep(long)) and all those simple tasks are stored in a container object that emulates a messageboard.
Each simple task has a dependency list of other tasks, and it cannot be executed until all of those tasks are completed.
How can I benefit from the Guava library using the ListenableFuture and the Futures.chain()?
I have searched everywhere for some extensive example code online, but didn't find anything that I understand how to use.
As Louis mentions, I think that Futures.allAsList etc. could be useful for you. However, I think that Futures.chain does seem useful and appropriate for the situation you describe. Since this is an assignment meant to challenge you, I'm not going to say any more than this: Futures.chain allows you to submit a task for execution upon completion of another task, and it returns a new ListenableFuture representing the result of that task. How does that apply to what you're trying to do?
You might be interested in reading the presentation slides on Guava util.concurrent, linked on the Guava homepage (slide 11 and onward). They really helped me crystallize my understanding of ListenableFuture and why it's so useful.
My guess is that the goal of your assignment is to understand how Futures work, and how Guava's ListenableFuture and Futures.chain() simplifies their use when coordinating multiple tasks.
The only open source code that comes to mind that uses ListenableFuture is sitebricks-mail:
MailClient interface
NettyImapClient implementation
I don't know if it uses Futures.chain(), though.
I don't think Futures.chain() is the answer here, if the primary issue is dealing with task dependency lists. More likely is Futures.allAsList or Futures.successfulAsList, which take multiple futures, and return a future that returns only after all of the input futures have succeeded or failed.
I'm working on developing a fairly robust 2D game engine as a base that other games can be built off of as a for-fun project (I know theres already things that do this, but that's no fun).
I'm trying to figure out a good way to do message-passing between classes within the engine. At first I was thinking about using a heirarchy of exceptions and throwing them whenever something required it. But as I was developing that way, I realized that there was quite a large number of exceptions being thrown, as they were being used for fairly common things (part of subroutines that handle pathfinding and unit locating and things that need to test the state of the game board alot). The exceptions were being used for things like the pathfinding came across a unit in the way and needed to go around it, it would throw a TileOccupied exception and the pathfinding could gracefully handle that and continue. As can be expected, this created a lot of exceptions.
The internet has told me that exceptions are expensive due to all the run-time processing they need to do. But they handle what I need perfectly, which is being able to propogate a message back to the caller even through branching subroutines to indicate when something has happened or something was not as expected.
Is there any clean/efficient way to do this in c++? Or am I structuring things very wrongly if I am using this type of notification? I'm still learning, so any suggestions would be greatly appreciated (and I'm willing to read / learn any references you can throw my way)
Edit
I'm trying to do this in standard c++ btw. I am writing it on linux, and want it to compile and be runnable platform-independent. I'm currently using Boost in it.
Although this requires explicit registration, this sounds like you want callbacks (eased by e.g. Boost.Function) or signals (like Boost.Signals/Signals2).
Is there any reason you haven't used an event queue and/or observer setup?
Exceptions are the wrong way of doing this. Usual suggestion would be direct events/listeners design, but that quickly gets out of hand within any non-trivial system. I'd point you to a whiteboard design for loose communications.
What I'm looking for is a simple timer queue possibly with an external timing source and a poll method (in this way it will be multi-platform). Each enqueued message could be an object implementing a simple interface with a virtual onTimer() member function.
Boost::ASIO contains an asynchronous timer implementation. That might work for you.
There is a nice article in CodeProject, here, that describes the various timers available in Windows, and has chapters titled "Queue timers" and "Make your own timer".
For platform independence, you'd have to make implementations for the different platforms inside #ifdef -- #endif pairs. I can see nothing less ugly than that.
It doesn't fit all of your criteria, but... I wrote a series of blog posts about a timer queue for windows that is implemented in terms of an external time provider and that is either polled or driven by a thread. The series comes with source code and tests and the point of it was to demonstrate the testing of reasonably complex code. Anyway, you might be able to make use of some of the code or ideas if nobody comes up with a better fit.
Articles start here: http://www.lenholgate.com/archives/000306.html
I've created a model for executing worker tasks in a server application using a thread pool associated with an IO completion port such as shown in the posts below:
http://weblogs.asp.net/kennykerr/archive/2008/01/03/parallel-programming-with-c-part-4-i-o-completion-ports.aspx
http://blogs.msdn.com/larryosterman/archive/2004/03/29/101329.aspx
Are there any classes in boost that can help with this programming model?
Not really, at least, not last time I looked. I mean, boost::thread_group might make things marginally tidier in places, but not so as would make much of a difference, I don't think.
Boost's thread support seems marginally useful when writing something that's cross-platform, but given that what you're writing is going to be Win32-specific anyway (due to the use of IOCPs) there doesn't really seem to be much benefit from that.
You might want to check out the threadpool project, which looks like a nice threadpool implementation on top of boost. I haven't tried it myself, but it looks fairly nice.
I haven't seen anything in boost that helps with the structure that you tend to end up with when using IO Completion Ports, but then I haven't looked that recently... However, slightly off-topic, you might like to take a look at the IOCP based thread pool that is part of my free IOCP server framework. It might give you some ideas if nothing else. You can find the code here. The thread pool supports expansion and contraction based on demand and has been in use in production systems for over 6 years.
ACE has some reactors that you can use to model things around your IOCPs. Some of these could have been added to boost, but boost makes building them pretty easy.