What is a stream in C++?

I know that in C++ there are no threads of their own, only the implementation through I/O, and there through winapi it seems, but how to represent a thread and what is it in general and in what memory is it located and at what point in time is it working or constantly working and what is multithreading and how to do it? In general, I do not know anything about streams, and in the books they are limited to a couple of words about them and an easy example of how to use them. It is clear that I still know very little, since I can not in the source code understand what's what. But could you give some easy understanding of the flows that does not require low-level knowledge, so that at least for a while you know what you are dealing with? And then for example in the links 1 and 2 a solid dark forest and nothing is clear.

Author: sercxjo, 2013-03-05

3 answers

First you need to understand that in the Russian language, the word "stream" translates two different terms. The first is the execution thread, aka thread, the second is the data stream, aka stream.

I know that C++ doesn't have its own threads

Incorrect. The latest standard already has execution threads. And data streams have been there for a long time. Both are ultimately implemented through the operating system.

But how to represent the flow (data)

Think of it as an additional program that shares a common address space (and some other resources).) with the main one. A shared address space is when a pointer from one thread will be valid in another. With programs, this is not the case. If you pass the value of the pointer (for example, through messages), it will point to an unknown destination (except for the situation with wm_copy_data).

At what point in time is it working or constantly works

Once the thread is started, it starts running until it finishes its work. To begin with, consider that it is executed in parallel. A bunch of running programs that run at the same time, do not confuse? It's the same with threads. Moreover, processes are not actually executed - they are just containers. Threads are executed. As a consequence, any process has at least one thread.

And then for example in links 1 and 2 a solid dark forest and nothing not clear.

I described the difference above.

Now about whether the data stream can 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?

The first step is 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 of.

In in general, your question neatly mixes questions about the two types of threads. I tried to separate and mostly answer about threads of execution (thread).

 11
Author: KoVadim, 2016-05-07 19:46:40

Here 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'm wrong about something, please do not aggra, but just correct me.

VERY ROUGHLY SPEAKING:

There is an app. When you run it, a process appears. A process is an instance of 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 stream can be one or there can be several of them. For example, if you want to draw the interface and load data from the database at the same time when you start the application, then you will need to divide these two operations into two threads - the interface will be on the main one, and data loading on the additional one.
go ahead
Which thread will run faster? For the processing of each process, the system allocates CPU time, depending on the priority of the process (you can view the priorities in the task manager). Each thread inside the process also has a priority, and according to these priorities, the allocated processor time will be divided between the threads.

 4
Author: teanЫЧ, 2013-03-05 11:48:27

Since the term "multithreading" is mentioned, I suggest another option for "easy representation" of execution threads.

How to represent a thread and what it is in general, at what point in time
{[2 is running]}

Any of your programs is a sequence of commands that the processor processes, 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 when you allow them, that is, when you allow them to work. You can start many threads at once, or they can be started if necessary on certain events, they can terminate after completing the tasks assigned to them, or they can exist for the entire life of the program. If you are asking a question like "why they were invented, there are processes", the answer is the following : switching the proetssor between different processes takes a lot of time due to the fact that the processes have their own memory space. When switching between threads, these costs are significantly less, since threads share most of the program's resources. The fact that each of the threads does not share with the others can clearly be attributed to the stack (the process stack is divided by the number of" contained in it " threads, each thread gets its own) and the program counter. In general, threads can be represented as multiple pieces of code running in parallel within a single process.

In what memory is it located

Since a thread of execution is essentially a piece of code then in program memory.

What is multithreading and how to do it

Multithreading-the property of program code to be executed in parallel (simultaneously) on multiple processor cores(by assigning each core a thread of execution(a piece of code that it should process)) or run pseudo-parallel on a single core(each thread gets some time at its disposal, in which it manages to execute a part its code on the processor). The distribution of threads across the cores is usually handled by the dispatcher, but if desired, you can explicitly bind the thread to any core in the program code.

You can make a program multithreaded using special library functions, for example, from Boost or Intel TBB. In the general case:

  1. run the thread creation function and pass it the code that the thread will execute(a pointer to its own function, method),
  2. using library resources synchronization functions in the codes of their functions/methods

There are also simpler ways, such as using the OpenMP API, which allows you to parallelize individual pieces of code simply by adding compiler directives, but this method has its own limitations in application and is not suitable for all tasks.

*this answer gives only a cursory view of the execution threads, as requested.

 4
Author: margosh, 2013-03-06 13:57:19