|
|
|
|
|
by tsimionescu
2222 days ago
|
|
> OK so Thread A calls “await” on a coroutine that executes in Thread B (where B may or may not be A). Thread A is now blocked on that coroutine. What have I gained by running that coroutine in Thread B? If the task that bar() will return is created when you first call it, then you're right, we didn't gain much. However, the task may have already been running for a long time behind the scenes, we may have done things in parallel with that run, and now that we need the result, we can block. For example: myHttpClient.StartReq1()
myHttpClient.StartReq2()
auto Res1 = await myHttpClient.WaitReq1()
auto res2 = await myHttpClient.WaitReq2()
> I’m pretty sure you could implement futures and async/await using Go channels too if you wanted to.Given the lack of generics, you would get a much worse interface. Btw, here is what a non-buffering channel would look like in Java: class Channel<T> {
T value;
void publish(T value) {
synchronized(this) {
this.value = value;
try {
this.wait();
} catch (InterruptedException e) {}
}
}
T consume() {
synchronized(this) {
this.notify();
return this.value;
}
}
}
|
|
So in Go that’s just a blocking channel receive.