What is thread?

What do t-shirts and computer programs have in common? They both consist of many threads! While the threads in the T-shirt hold the fabric in the form of a single fabric, the C Thread (literally, “threads” or “threads”) of the operating system combines all the programs in order to perform sequential or parallel actions simultaneously. Each thread in the program identifies the process that starts when the system (system Thread C) requests it. This optimizes the operation of such a complex device as a personal computer and positively affects its speed and productivity.

Definition

In computer science, C Thread, or the thread of execution, is the smallest sequence of commands that are controlled by an independent scheduler, which is usually an integral part of the operating system.

c thread

Threads are usually given a certain priority, which means that some threads take precedence over others. As soon as the processor completes processing one thread, it can start the next one waiting for the queue. As a rule, the wait does not exceed a few milliseconds. Computer programs that implement multithreading can execute multiple threads at once. Most modern operating systems support C Thread at the system level. This means that when one program tries to take all the processor resources, the system forcibly switches to other programs and forces the processor support program to share resources evenly.

The term “thread” (With Thread) may also refer to a series of related posts in an online discussion. Web bulletin boards consist of many topics or branches. Responses sent in response to the original publication are part of the same stream. In an email, a thread can refer to a series of responses in the form of “back” and “forward” commands related to a particular message and structure the conversation tree.

C Threading on Windows

In computer programming, single-threaded processing is processing one command at a time. The opposite of single-threading is multi-threading. Both terms are widely used in the functional programming community.

Multithreading is similar to multitasking, but it allows you to process several threads at once, but not several processes. Since threads are smaller and controlled by simpler instructions, multithreading can also occur within processes.

C Thread task tool examples

A multi-threaded operating system can simultaneously perform several background tasks, such as logging file changes, indexing data, and managing windows. Web browsers that support multithreading can open multiple windows while running JavaScript and Flash. If the program is fully multithreaded, different processes should not affect each other if the processor has enough power to process them.

Like multitasking, multithreading also improves program stability. Multithreading can prevent a program from crashing and prevent a computer from crashing. Since each stream is processed separately, an error in one of them is not able to disrupt the PC. Thus, multithreading can lead to fewer crashes in the operating system as a whole.

thread sleep c

Multitasking

Multitasking processes several tasks in parallel and also characterizes the principles of computer operation. A processor can process several processes at once with absolute accuracy. However, it only processes instructions sent to it by the software. Therefore, in order to fully utilize the capabilities of the CPU, the software must be able to process more than one task at a time and also be multitasking.

Historical retrospective

Early operating systems could run several programs at once, but did not fully support multitasking. One program could consume all processor resources during a certain operation. The main tasks of the operating system, such as copying files, did not allow the user to perform other tasks (opening or closing windows, for example).

Modern operating systems include full support for multitasking - several software solutions can work simultaneously without interfering with each other's functionality.

Multitasking also improves computer stability. For example, if one of the processes fails, it will not affect other running programs, since the computer processes each process separately. This can be compared to the process of writing a letter: if you are in the middle of a sheet and have already written some of the text, but your web browser unexpectedly quits, then you will not lose the work already done.

c thread task

Single and multiprocessor systems

The implementation of streaming and processor technologies differs, depending on the operating system, but most often the stream is a component of the process. At the same time, several threads are able to exist in a single process, executing and sharing resources. In particular, C Thread process threads share executable code and variable values ​​at any given time.

Systems with a single processor implement multi-threading in time: the central processing unit (CPU) switches between different software streams. In a multiprocessor, as well as in a multi-core system, a certain number of threads are executed in parallel mode, with each processor or core executing a separate thread at the same time.

Types of streams

The process schedulers of most modern operating systems directly support both temporary and multi-processor streaming processing, while the kernel of the operating system allows developers to manage threads by providing the necessary functions through the system call interface. Some threading implementations are called kernel threads, while light processes (LWP) are a type of thread that has the same information state. Software solutions can also have user space streams when they are used with timers (Thread timer C), signals, or other methods to interrupt their own execution by performing a kind of ad hoc timing.

Threads and processes: differences

Threads differ from the classic processes of multitasking OS in the following characteristics:

  • processes are usually independent, while threads exist as subsets of a process;

  • processes carry much more information than flows;

  • processes have dedicated address spaces;

  • processes interact only through system communication mechanisms;

  • context switching between threads in a process is faster than context switching between processes.

Proactive and collaborative planning

In multi-user operating systems, proactive multithreading is a more widely used approach for controlling runtime by context switching. However, proactive planning can lead to uncontrolled programmer prioritization and crashes. In contrast, collaborative multithreading relies on threads to abandon execution control. This can be a problem if the joint multitasking thread is blocked by waiting for a resource.

c thread join

Technology evolution

Until the early 2000s. on most desktop computers there was only one single-core processor that did not support hardware threads. In 2002, Intel implemented support for simultaneous multithreading on the Pentium 4 processor, which is called Hyper-Threading. In 2005, a dual-core Pentium D processor and a dual-core AMD Athlon 64 X2 processor were introduced.

Processors in integrated systems with higher requirements for real-time actions are capable of supporting multithreading, reducing thread switching time, and use a dedicated register file for each thread.

Models

We list the main implementation models.

1: 1 (thread at kernel level) - threads created by the user in the kernel are the simplest possible implementation of threads. OS / 2 and Win32 use this approach initially, while in the Linux library, the C thread join implements this approach through NPTL or older LinuxThreads. This approach is also used by Solaris, NetBSD, FreeBSD, macOS, and iOS.

N: 1 (user thread) —This model provides that all application-level threads are mapped to one scheduled kernel-level object. With this approach, context switching can be performed very quickly, and, in addition, it can even be implemented on those cores that do not support stream processing. However, one of the main drawbacks is that it does not benefit from hardware acceleration on multi-threaded processors or computers. For example: if one of the threads needs to be executed when I / O is requested, the whole process is blocked and the streaming cannot be used. GNU Portable C Thread exception is used as user level threading.

M: N (hybrid implementation) - the model displays a number of application threads for some N number of kernel cells, or “virtual processors”. This is a compromise between kernel-level (“1: 1”) and user (“N: 1”) threads. “M: N” streaming systems are more complex because changes are required to both the kernel code and the user code. In the M: N implementation, the threading library is responsible for scheduling threads in the available scheduled entities. This makes the context the most optimal, since it allows you to avoid system calls. However, this increases the complexity and likelihood of inversion, as well as suboptimal planning, without extensive (and expensive) coordination between the user environment scheduler and the kernel scheduler.

std thread c

Examples of a hybrid implementation are the scheduler activation used by the embedded implementation of the POSIX NetBSD library (for the M: N model, as opposed to the 1: 1 kernel implementation model, or user space model).

Lightweight processes used by older versions of the Solaris operating system (Std Thread C toolkit).

Support for programming languages

Many formal systems support stream functionality. C and C ++ implementations implement this technology and provide access to their own APIs for the operating system. Some higher-level programming languages, such as Java, Python, and the .NET Framework, expose threads to developers when abstracting specific differences in the implementation of threads in the runtime. Other language extensions are also trying to abstract the concept of concurrency and streaming from the developer. Some languages ​​are designed for sequential parallelism using GPUs.

windows c thread

A number of interpreted languages ​​have implementations that support streaming and parallel processing, but not parallel execution of threads due to global interpreter lock (GIL). GIL is a mutual exclusion blocking executed by the interpreter, which can interfere with the simultaneous interpretation of application code on two or more threads at the same time, which limits concurrency in multi-core systems.

Other implementations of interpreted programming languages , such as Tcl, use the Thread sleep C extension. This avoids the maximum GIL limit by using a model where content and code must be explicitly “shared” between threads.

Event-oriented application programming languages ​​such as Verilog and the Thread sleep C extension have a different thread model that supports the maximum number of threads for modeling equipment.

c thread exception

Practical multithreading

Multithreaded libraries initiate a function call to generate a new thread that takes a functional value as a parameter. Then a new parallel thread is created and processing of the launched function is started, followed by return. Programming languages ​​contain stream libraries that include global synchronization functions that allow you to create and successfully implement multithreading with the condition that there are no errors using mutexes, variable conditions, critical sections, monitors, and other types of synchronization.

Source: https://habr.com/ru/post/K7065/


All Articles