I know that in C ++ there are no threads of my own, only the implementation through I / O, and there it seems through winapi, but how to represent the flow and what it is in general and in what memory it is located and at what point in time it works or constantly works and what is multithreading and how to do it? In general, I don’t know anything about streams, but in books I’m limited to a few words about them and an easy example of how to use them. It is clear that I still know very little, because I can not understand in the source code what is what. But could you give some easy idea of the flows that do not require low-level knowledge, so that at least for a while you know what you are dealing with? And then for example in references 1 and 2 a solid dark forest and nothing is clear.
- 2@mzarb, the link (1) is thread, and (2) is stream. Thread - processor instruction flow. Stream - I / O byte stream. Between them and the Russian word flow has nothing in common. - avp
- 2@mzarb, judging by your question. Why is it impossible to overload the operator << in the class? it's worth clarifying that you are primarily interested in I / O streams in C ++ . Otherwise, the treads torture (now it is fashionable). - avp
3 answers
First you need to understand that in Russian one word “stream” translate two different terms. The first is the stream of execution, it’s also thread , the second is the stream of data, it’s stream .
I know that in C ++ there are no streams
Wrong. In the last standard, execution threads are already there. And there are data streams there for a long time. Both that, and another finally is realized through an operating system.
but how to represent the stream (data)
Present it as an additional program that shares the common address space (and some other resources) with the main one. The total address space is when the pointer from one thread is valid in another. With the programs it is not. If you pass the pointer value (for example, through messages), it will indicate incomprehensibly where (except for the situation with wm_copy_data
).
at what point in time is working or constantly working
After the thread is started, it starts working until it completes its work. To begin with, consider that it is executed in parallel. A bunch of running programs that run simultaneously, do not bother? With threads the same. Moreover, in fact, processes are not executed - they are just containers. Threads are executed. As a result, any process has at least one thread.
And then for example in references 1 and 2 a solid dark forest and nothing is clear.
I described the difference above.
Now that can the data stream be executed. Theoretically, yes, if it is a stream of bytes that goes to the input of the processor. But in C ++, it can only be read-written.
How to do it if you need a data stream?
First you need to create a function that will be executed in a separate thread. Then call the appropriate function of the operating system ( _beginthread
, CreateThread
and the like). Here is an example .
In general, in your question questions about the two types of flows are carefully mixed. I tried to split and basically respond about threads of execution ( thread ).
- That is, processes are address spaces with a container for threads (thread), and data streams (stream) is already an abstract entity of a programming language, which ultimately becomes thread? I mean that stream is implemented using thread and is there just a shell for usability? - mzarb
- one>> That is, processes are address spaces with a container for execution threads (thread) yes >> and data streams (stream) are already an abstract essence of a programming language, well, almost abstract. film in cassettes remember? this is also a data stream. Well, or news feed in VKontakte. It is also stream. >> which eventually becomes thread? I mean that stream is implemented using thread and is there just a shell for usability? not. - KoVadim
- one@mzarb, in fact your question in the comment, I would say that stream and thread are not related . Those. communication between them with tz programming logic is no more than between the registers of the processor and the buffers of a controller. Indeed, in the implementation of both the registers and the buffer memory there are transistors (in the form of modified sections of a silicon wafer) and between them there are definitely connected current conductors. - avp
- @avp in common between the registers of the processor and the buffers of the controller is much larger. - alexlz pm
there is a large and detailed description of the streams (STREAM) in C ++
Threads Thread
Now there will be an essay on the topic "how I spent my student years." If I am mistaken in something, please do not aggrivate, but just correct me.
VERY GROSSLY TALKING:
There is an application. When you start it, a process appears. A process is an application loaded into RAM. Each process consists of threads, you can even say that the procedures / functions of processes are divided between threads. The flow may be one or there may be several. For example, if you want the application to start, you simultaneously draw the interface and load data from the database, then you will need to divide these two operations into two streams - the interface will be on the main one, and the data will be loaded on an additional one.
go ahead
Which thread will run faster? The system allocates processor time to the processing of each process, depending on the priority of the process (you can see the priorities in the task manager). Each thread within the process also has a priority, and according to the priorities, the allocated processor time will be divided between the threads.
- the link describes slightly other streams :) - KoVadim
- @KoVadim, yes, there are data streams. But the author in question and data streams and process flows. According to the link, I described some, and in the others - teanYCH
- but this is not a reason to take an example from the vehicle and mix it up :) - KoVadim
- I had a flight of fancy :) I first wrote everything I remember, and then edited and shared :) - teanYCH
Since the term "multithreading" is mentioned, I propose another version of the "easy view" of execution threads.
how to represent the flow and what it is, at what point in time
Any program of yours is a sequence of instructions processed by a processor in a single-threaded program - this sequence is one per process, in a multi-threaded program there are many of them. They work at the moment in which you allow them, i.e. You can start many threads at once, or they can be launched, if necessary, on certain events, they can be completed by fulfilling the duties assigned to them, or they can exist for the entire lifetime of the program. If you ask a question such as “why they were invented, there are processes”, the answer is the following: it takes a lot of time to switch the projectors between different processes because the processes have their own memory. When switching between threads, these costs are significantly lower, since the threads share most of the program resources. To the fact that each of the threads does not share with the others, the stack can be unambiguously attributed (the process stack is divided by the number of threads contained in it, each thread gets its own) and a program counter. In the general case, threads can be represented as several pieces of code running in parallel in a single process.
what memory is it in
since the thread of execution is essentially a piece of code, it is in the program memory.
what is multithreading and how to do it
multithreading is a property of the program code to run in parallel (simultaneously) on several processor cores (by assigning each core a thread of execution (a piece of code that it must process)) or run pseudo-parallelly on one core (each thread has at its disposal some time during which it manages to execute part of his code on the processor). The distribution of threads among the cores is usually handled by the dispatcher, but, if desired, in the program code, you can explicitly bind the thread to a core.
You can make a multi-threaded program using special library functions, for example, from Boost or Intel TBB. In general:
- start the function of creating a thread and pass to it the code that the thread will execute (pointer to its own function, method),
- we use library synchronization functions in the codes of our functions / methods
There are also simpler methods, for example, using the OpenMP API standard, which allows parallelization of individual pieces of code simply by adding compiler directives, but this method has its own limitations and is not suitable for all tasks.
* This answer gives only a superficial idea of the execution threads, as requested.