Calling StructuredTaskScope.join() several times inside the same scope

Remi Forax forax at univ-mlv.fr
Mon Sep 4 06:38:59 UTC 2023


Resent because it seems that the mailing list has some issue.

---

Hi all,
I've a problem with the actual semantics of StructuredTaskScope.shutdown()/join().

Let say, i want to get the result of the first two tasks and then the result of the next two.
I can us the fact that I call call join() several times and write

        try(var sts = new StructuredTaskScope<>()) {
            var task1 = sts.fork(() -> 1);
            var task2 = sts.fork(() -> 2);
            sts.join();
            System.out.println(task1.get() + " " + task2.get());  // 1 2

            var task3 = sts.fork(() -> 3);
            var task4 = sts.fork(() -> 4);
            sts.join();
            System.out.println(task3.get() + " " + task4.get());  // 3 4
        }

But let say I want to the same with a ShutdownOnSucess()

        try(var sts = new StructuredTaskScope.ShutdownOnSuccess<>()) {
            var task1 = sts.fork(() -> 1);
            var task2 = sts.fork(() -> 2);
            sts.join();
            System.out.println(sts.result());  // 1

            var task3 = sts.fork(() -> 3);
            var task4 = sts.fork(() -> 4);
            sts.join();
            System.out.println(sts.result());  // 1 <--- ???
        }

This does not work because ShutdownOnSuccess uses shutdown() and shutdown() is an event global to a scope, not an event which is local to one of the call to join().

So being calling several join() inside a scope only works with a plain StructuredTaskScope but not with a ShutdownOnSuccess or a ShutdownOnFailure.
It's even worth than that, the exact semantics depend on if the subclasses actually call shutdown which depends if the individual task threads succeed or not, so it's a runtime condition, not one that can be predicted just taking a look to the code apart in contrived examples as the one above.

I think this semantics is confusing, because the semantics of join() is not the same depending on the subclass used.
To fix that, there are two choices, either being able to call only one join() or shutdown() to works multiple times.

I think I prefer the former to the latter, because it makes the semantics simpler and the code simpler to read, if you want to call join() twice, creates two scopes.

regards,
Rémi


More information about the loom-dev mailing list