task then() continuation among threads?

Oct 31, 2014 at 8:51 AM
I wonder if a task then() call will be serialized among threads.

For example,
  auto tt = create_task([] { return xx; }
Thread A:
  tt.then([](XX xx) { xx->yy(); });
Thread B:
  tt.then([](XX xx) { xx->zz(); });
I expected zz() will be called after yy() ended (or zz then yy),

but yy() and zz() executed simultaneously.

Is task then() continuation not guaranteed among threads?
Oct 31, 2014 at 9:42 PM
Hi sunghun,

Yes what you have noticed is correct about zz() and yy() being executed simultaneously. We fully intended it to function that way. If you want them to be serialized you could setup the task continuations as such. With something like the following:
auto tt = create_task([] { return xx; });
auto threadA = tt.then([](XX xx) { xx->yy(); return xx; });
auto threadB = threadA.then([](XX xx) { xx->zz(); });
Another option of course is you can always implement your own synchronization, with a lock for example, to only allow one of them to execute at once.

For more about programming with tasks I also recommend you take a look here.

Nov 1, 2014 at 2:15 PM
Maybe this queueing sample is an alternative solution: