A
A
Akaero2018-03-02 16:55:29
.NET
Akaero, 2018-03-02 16:55:29

Do I understand TPL and asynchrony correctly?

Good Friday everyone, folks!
I deal with TPL and asynchrony, I would like to check if I understand some points correctly.
Please critique, preferably with an explanation.
If you know tasks / questions for understanding the logic of this topic, share the links, thanks!
1. When using Task, it is not necessary that the task in this task is executed in a new thread.
2. Tax is executed in a new thread if the asynchronous method returns return Task.Run().
3. Asynchronous I/O operations do not use an additional thread.
4. Asynchronous CPU-bound operations are performed in a new thread. They have to work somewhere.
5. TaskCompletionSource<> is used to manually create tasks to wrap code based on old asynchrony patterns. For example, there is an asynchronous operation based on the event pattern, we subscribe to the completion event, at the end of the event we set the result of the Task.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Makarov, 2018-03-03
@Akaero

1. Yes, the task can be executed anywhere. Moreover, you won’t believe it, but some tasks may not be executed anywhere at all, because. there is nothing to perform (see paragraph 5). A task is, first of all, an abstraction of a value that will be received in the future, and to obtain which you may need to calculate something or just wait. See futures and promises .
2. Well, not necessarily in a new thread, it can be executed in an existing thread if there is a free one in the thread pool.
3. Asynchronous I/O operations are so named because they are performed through the operating system's asynchronous API. There is no point in using an asynchronous operation, but doing it in a separate thread created specifically for this operation. Threads are needed primarily in order to efficiently load executors, i.e. processors (including their various cores).
4. It is possible in a new one, it is possible in a thread from the pool. One way or another, if the operation involves just calculations, and not waiting for I / O, then this is certainly the case when it makes sense to shift the load to another thread.
5. If you think of a task as one of the sides of the "pipe" through which the result of an asynchronous operation arrives, then Task is the receiving side, and TaskCompletionSource is the transmitting (result-producing) side. When you create a task from code that needs to be executed, that code becomes the sender. But Task abstracts any asynchronous operation, not only the one where you need to read something for a long time. Perhaps you need to wait for one byte from the network, and the operation will be considered completed. In all those cases when you want to independently "produce" the result for some task (and give the task itself "outside" to other code, for example, return from a function), you can use TaskCompletionSource.
(By the way, in C++ the receiving side is called a future ,promise . In JS, the receiver is called a Promise and the transmitter is a functor).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question