Answer the question
In order to leave comments, you need to log in
How to create an asynchronous task and how is it different from a thread?
On Habré I read an article about threads Here is a link
Here is what I read:
1. As I understand it, synchronous single-threaded is a standard, ordinary thread with methods that are executed one after the other?
2. Here I did not understand how it can be synchronous multi-threaded? Isn't multithreading asynchronous?
3. Here is asynchronous single-threaded - how is asynchrony created in one thread?
4. Well, here is the same question, how is asynchrony created?!? Isn't creating a new thread asynchronous??!
Answer the question
In order to leave comments, you need to log in
1. something like this
2. No, multithreading has nothing to do with asynchrony. With multithreading, usually the kernel scheduler (if the threads are kernel) switches the context between your threads. It works like this: in your application, you tell the scheduler that you want to create a thread (for example, through pthread_create, if you are programming in linux or freebsd), indicating to it the function that should be executed in a separate thread and the arguments for it. The kernel creates an entry for it in its tables and queues this thread for execution in the same way as the main thread. They will have a common address space.
3. For example, you want to serve TCP clients, you create a socket (or several), set them to non-blocking mode, then you need to poll them in a loop. There are several ways to do this: select and poll are available in all OSes, but they are good when you have few sockets. Linux also has epoll which is much more efficient, FreeBSD and MacOS have kqueue which is a very efficient method. When, for example, a TCP client comes to you, your select/poll/epoll/kqueue will return information about the socket that is ready for reading (your listen socket), you make it accept, and add the client socket to select/poll/epoll/kqueue . If an event occurs on a client socket, for example, read worked on it, then there is data there, you read and process it, if write worked, the socket was freed for writing and you can write to it. Also kqueue and epoll have timers. The main thing is not to use any sleep with such an architecture, not to use blocking libraries with databases, you need to process everything quickly and return control to select/poll/epoll/kqueue. If, for example, you need to make a delay, the execution of the callback must be interrupted by creating some kind of timer (there are many ways).
In general - developing asynchronous applications requires a completely different approach, it is usually a bit more complicated, especially if you need to go to, for example, a database. For example, the system resolver (gethostbyname) is blocking - it cannot be used in such an architecture, you will have to take a library, for example, c-ares, which implements an asynchronous resolver.
4. the same as 3, but we run select/poll/epoll/kqueue in several threads, it is rational to do this to utilize several processors.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question