[cpp-threads] Asynchronous Function Proposal

Doug Lea dl at cs.oswego.edu
Thu Jun 4 12:15:56 BST 2009


Boehm, Hans wrote:
>> async() isn't meant to be The Answer (or even An Answer) for nested
>> parallelism, performance, or scalability. async() is about just providing a
>> single simple way to easily perform an async call and get an async result,
>> without all the packaged_task boilerplate people have to write otherwise in
>>  the current draft. It is mostly about concurrency rather than parallelism,
>> but there seems to me to be no reason to not also permit running a task on
>> a work stealing implementation, and the only hook I've been pushing for to
>> permit that is to have the default be "might or might not run on this
>> thread."
> I no longer believe that the difference between "concurrency" and
> "parallelism" is well-defined in general.  But if I understand correctly, I
> think we have to be careful here.  "Concurrent" applications are likely to
> use other forms of synchronization, which tend to be dangerous in fork-join
> frameworks.  I think you probably want only the "always create a thread"
> variant for those.

I agree that you do need to be careful -- lightweight task
(fork-join etc) frameworks are not designed for async tasks
that do a lot of arbitrary blocking on sync or IO. (Usually,
such tasks can be accommodated, but only with a lot of
time/space wastage.)

However, you can do a little better than always creating
a thread by using a caching thread pool. As in the one java
supplies in java.util.concurrent.Executors.newCachedThreadPool.
This reuses a thread if one previously created is available,
else creates one. Ours also bounds the keep-alive interval
to kill off those that have not been used for a while.

Implementing these is a little more fun than it looks.
You need some form of synchronous-queue or a related
handoff mechanism to get the data to the thread.

It is also possible for underlying OS-level libraries to do
something like this themselves upon pthread_create or whatever,
in which case the C++ library version wouldn't bother.


-Doug



More information about the cpp-threads mailing list