Possible language changes

Doug Lea dl at cs.oswego.edu
Sat Mar 5 01:02:03 GMT 2005


The combination of replies from Kevlin, Hans, and Doug Schmidt put me
in the surprising position of suggesting that we (you) consider
adopting something straight out of java.util.concurrent:

An "Executor" is a something that can run code (submitted as
    closures. In Java Runnables and Callables). One implementation
    of an Executor is one that spawns a thread on each call to
    execute. Another is a thread pool. Another
     is something that just invokes the code in the current thread.
A Future represents the results of that action, that you can
     retrieve, wait for, and/or try to cancel.
An ExecutorService is an Executor that has a controllable
    lifecycle -- you can shut it down etc, -- and can in general
    support execution of many actions

The advantages here of something along these lines are that it
punts on what exactly consititutes a Thread; thus bypassing
the issue that is least likely to find consensus.
A C++ system could support this API without ever exposing what it
considers to be a thread. However, conversely, all existing thread-based
subsystems can fairly easily support this API.

Java Interfaces are below. The full versions are in your local
javadocs, or the defintitive source: 
http://gee.cs.oswego.edu/dl/jsr166/dist/docs/

I don't pretend that exactly this API makes sense for C++,
so haven't tried to translate.
And I'm not even particularly advocating this. It just struck me
as alternative that more people could agree on than the others
we've discussed so far.



public interface Executor {
     void execute(Runnable command);
}

public interface Future<V> {
     boolean cancel(boolean mayInterruptIfRunning);
     boolean isCancelled();
     boolean isDone();
     V get() throws InterruptedException, ExecutionException;
     V get(long timeout, TimeUnit unit)
         throws InterruptedException, ExecutionException,
                TimeoutException;;
}

public interface ExecutorService extends Executor {
     void shutdown();
     List<Runnable> shutdownNow();
     boolean isShutdown();
     boolean isTerminated();
     boolean awaitTermination(long timeout, TimeUnit unit)
         throws InterruptedException;
     <T> Future<T> submit(Callable<T> task);
     <T> Future<T> submit(Runnable task, T result);
     Future<?> submit(Runnable task);
     <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks)
         throws InterruptedException;
     <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks,
                                   long timeout, TimeUnit unit)
         throws InterruptedException;
     <T> T invokeAny(Collection<Callable<T>> tasks)
         throws InterruptedException, ExecutionException;
     <T> T invokeAny(Collection<Callable<T>> tasks,
                     long timeout, TimeUnit unit)
         throws InterruptedException, ExecutionException,
                TimeoutException;
}







More information about the cpp-threads mailing list