Threading (Code implementation)

Source: Internet
Author: User
Tags mutex

In computer science, a thread executes a system that can be independently dispatched by a minimal sequence of administrative programming instructions, which is the usual part of the OS. A thread differs from the operating system described in the implementation process, but in most cases, a thread is part of the process. Multiple threads can exist in a process, performing simultaneous and shared resources, such as storage, while different processes do not share these resources. In particular, a thread of a process shares its executable code and its variables at any given time value.

A software thread that has a single-processor system that typically implements switching between multiple multithreaded time shards: the central processing Unit (CPU). This context switch usually occurs very frequently and is quickly enough for a user-aware thread or task to run in parallel. In a multiprocessor or multicore system, multiple threads can execute parallel to each processor or core while executing a separate thread; A hardware thread on a processor or core, a standalone software thread, or a separate hardware thread can be executed at the same time.

The layout made a variable number of pre-appearing os/360 multi-channel processing tasks (MVT) in 1967, in the context of which they were called "quests". The term "thread" has been attributed to Victor A. Vyssotsky. Processing scheduling many modern operating systems directly support time shards and multiprocessor threads, and the operating system kernel allows programmers to manipulate thread system invocation interfaces by exposing the required functionality. Some thread implementations are called Kernel threads, while the lightweight process (LWP) is a specific type of kernel thread that shares the same state and information. In addition, the program can have user space threads with timers, signals, or other methods when the thread interrupts its own execution, performing a sort of temporary time shard.

Threads and Processes
Threads traditionally different multi-tasking operating system processes are:
* Processes are usually independent, while threads exist as a subset of a process
* The process takes a considerable amount of state than the thread's information, while a process shares the process of multiple threads and memory resources in the state
* Processes have separate address spaces, while threads share their address space
* Processes can only communicate through system-provided interactions between interprocess communication mechanisms
Context switches are usually faster than the context process between the same process threads.
Windows NT and OS/2, such as the system, are said to have inexpensive threading and expensive processes; In other operating systems there is an exception to the cost of such a large difference in the address space of the switch, on some systems (especially 86), the result is aligned in the conversion fallback buffer (TLB).


Single Thread
In computer programming, a single thread is processed one command at a time.

Multithreading
Multithreading mainly exists in multi-tasking operating system. Multithreading is a common programming and execution model that allows multiple threads to exist in the context of a process. These threads share the resources of the process, but are able to execute independently. The threading programming model provides a useful abstraction for developers to perform concurrently. Multithreading can also be applied to a process to enable parallel execution of a top-multiple system.

Multithreaded applications have the following Advantages
Response: Multithreading allows applications to remain responsive input. In a threaded program, if the main execution thread block of a long-running task, the entire application can appear frozen. This is possible by moving such long-running tasks to a worker thread running concurrently with the primary execution thread, while the application that performs the task in the background remains responsive to user input. On the other hand, in most cases multithreading is the only way to not keep a program response, with non-blocking I/O and/or UNIX signals that can be used to obtain similar results.
Faster Execution: This advantage of multithreaded programs allows it to operate faster on a computer system with multiple central processing units (CPUs) or one or more multicore processors, or cross-cluster machines, because the threads of the program naturally lend themselves to parallel execution, assuming that there is sufficient independence (i.e. they do not need to wait for each other).
Lower resource consumption: Using threads, an application can use fewer resources for multiple clients at the same time than a process that uses its own multiple copies. For example, Apache's HTTP server uses the thread pool: The listener process listens to incoming request pool and server threads to process these request pools.
Better System Utilization: As an example, using multiple threads can be implemented because data in faster media (such as cache memory) has higher throughput and lower latency one file system can be retrieved by one thread, while another thread retrieves data from slower media (such as retrieving as external storage) with no threads waiting for another to complete.
simplified sharing and communication: With processes, which require a messaging or shared memory mechanism to perform interprocess communication (IPC), threads can pass data, code, and they have shared file traffic.
parallelization of: You want to use multi-core or multi-CPU systems that can use multithreaded data and tasks to split into sub-task parallelism, allowing the application of the underlying architecture to manage how threads run, whether in parallel on one core or multiple cores. Like the GPU computing environment Cuda and OPENCL use multithreaded models, where dozens of to hundreds of of threads run parallel across a large number of cores on the data.

Multithreading has the following Disadvantages
Sync: Because threads share the same address space, programmers must be careful to avoid competitive conditions and other non-intuitive behavior. In order to manipulate the data correctly, threads will often need rendezvous time to process the data in the correct order. Threads may also require mutually exclusive operations (typically implemented using semaphores to prevent simultaneous modification or reading of the modified process, while common data). Careful use of this primitive may lead to deadlocks.
the process of a theme crash: The entire process by which a thread performs an illegal operation crash; Therefore, a running exception thread can break the processing of all other threads in the application.

Scheduling
The operating system scheduler thread is either preemptive or collaborative。 Preemptive multithreading is generally considered an excellent method because it allows the operating system to determine when a context switch should occur. The disadvantage of preemptive multithreading is that the system can switch back and forth in an inappropriate time, resulting in lock fleets, priority reversals, or other negative effects, which can be avoided through cooperative multithreading. Collaborative multi-threading, on the other hand, relies on the thread itself to give up control once they are at a stop point. This can create an issue if a thread waits for resources to become available.

In the processor's embedded system, it has higher requirements for real-time behavior, which may be reduced by reducing the thread switching time, perhaps by assigning a dedicated support for each thread in a multithreaded register file instead of saving/recovering the Universal register file.


processes, kernel threads, user threads and fibres
Scheduling can take place at the kernel level or at the user level, and multitasking can be done either proactively or collaboratively. This creates a variety of related concepts.

At the kernel level, a process that contains one or more kernel threads that share a process's resources, such as memory and file handles-a procedure is a unit of resources, while a thread is dispatched and executed. Kernel scheduling is usually even preemptive, or infrequently used, to cooperate. Processing at the user level, such as the runtime system itself, can schedule multiple threads of execution. If these do not share data, such as in Erlang, they are usually similar to those called processes, and if they share data, they are often referred to as (user) threads, especially if preemptive scheduling. Collaborative scheduling of user threads is called fiber; Different processes may have different scheduling user threads. User threads can be executed in a variety of ways by kernel threads (one, many to ketones, and many to many). The so-called "lightweight process" refers to a user thread or kernel mechanism that dispatches a user thread to a kernel thread.

A process is a "heavyweight" unit of kernel scheduling, which is relatively expensive to create, destroy, and switch processes. The process has resources assigned by the operating system. Resources include memory (code and data), file handles, sockets, device handles, Windows, and a Process control block. The process is separated by process isolation, and does not share the address space or file resources unless through explicit methods such as inheriting file handles or shared memory segments, or mapping the same file in a shared way-see inter-communication. The creation or destruction process is relatively expensive because resources must be obtained or disposed of. The process is usually preemptive multitasking, and process switching is relatively expensive, exceeding the basic cost context switch due to issues such as cache refreshes.

A "lightweight" unit of kernel scheduling for a kernel thread. At least one kernel thread is present in each process. If multiple kernel threads can exist in a process, they share the same storage and file resources. If the kernel thread is preempted by preemptive multitasking during the operating system, it is pre-emptive. Kernel threads do not own resources, except for a stack, where the register includes program counters, as well as thread-local storage (if any), and is therefore relatively inexpensive to create and destroy. Thread switching is also relatively inexpensive: it requires a calendar switchover (saving and restoring registers and stack pointers), but does not alter the virtual memory, so the cache type (outgoing TLB is invalid). The kernel can assign a thread to each logical core in a system (because each processor splits itself into multiple logical cores to support multiple threads, or only supports each if there is no physical core of a logical kernel), and the swap thread is blocked. However, kernel threads need to be much longer than the user thread to swap.

Threads are executing, and sometimes the user space library is called a user thread. The kernel is not aware of them, making them manageable and planning in user space. Some implementations are based on several kernel threads on top of the user thread, benefiting from the multiprocessor machine (m:n mode). In this article, the term "thread" (no kernel or user qualifier) defaults to the reference kernel thread. A virtual machine that is implemented as a user thread is also known as a green thread. User threads are generally quick to create and manage, but cannot take advantage of multithreading or more if all of their related kernel threads are blocked, even if some of the user threads are ready to run will be blocked.

Fiber is a lighter unit of dispatch, and they are co-operative: A running fiber must be explicitly "profitable", allowing another fiber to run, which makes them easier to implement than the kernel or user thread. A fiber can be scheduled to run in any thread in the same process. This allows the application to schedule itself through management, rather than relying on kernel scheduling (which may not be tuned to the application) for performance gains. In parallel programming environments, such as OpenMP, it is common to implement their tasks through fiber optics. A closely related fiber is a synergistic process, and the difference is that the co-process is a language-level construct, and the fiber is a system-level structure.


concurrency and data Structures
Share the same address space on the same process thread. This allows the code to run concurrently, the couple tightly and without the overhead and complexity of conveniently exchanging the data for the IPC. When shared between threads, however, even simple data structures become prone to race conditions two threads may eventually attempt to update the data structure at the same time, and discover that it accidentally changes the foot: if they need more than one CPU to update the instructions. Errors that cause competitive conditions can be very difficult to replicate and isolate.

To prevent this, the thread application Programming interface (API) quotes the synchronization primitives, such as mutexes, to lock the data structure against concurrent access. On a single-processor system, a mutex that runs to a lock must sleep, and therefore trigger a context switch. In a multiprocessor system, a thread can instead poll a mutex spin lock. Both of these may weaken the performance and force of the processor's symmetric multi-processing (SMP) system to compete with the memory bus, especially if the granularity of the lock is good.

I/O and scheduling
Netizens thread or fiber optic implementations are usually completely in user space. As a result, it is very efficient to switch between user threads or fibers within the same process as the context, because it does not require any interaction with the kernel at all: a context switch can be performed or fibres by storing the CPU registers that are used by the currently executing user thread locally. The registers required to load the user thread or fiber are then executed. Due to scheduling in user space, the scheduling policy can be more easily adapted to the requirements of the program's workload.


However, using a blocking system call on the user thread (relative to the kernel thread) or fiber can be problematic. If a user thread or fiber performs a system call, in the process block, other user threads and fibers cannot be run until the system call returns. A typical example of this problem is when I am performing I/O: Most programs are written synchronously to perform I/O. When the I/O operation is initiated, the system is called and does not return until the I/O operation is complete. During this time, the entire process was "blocked" by the kernel and could not run, which starved other user threads and fibers in the execution of the same process.


A common one of this problem?? The solution is to provide an I/O API that implements the synchronization interface by using non-blocking I/O and dispatching another user thread or fiber, while I/O operations are in progress. Similar solutions can be provided for other blocking system calls. Additionally, the program can be written to avoid using synchronous I/O or other interception system calls.

The kernel thread that is used simplifies the user's code by moving some of the most complex problems that wear into the kernel. The program does not need to dispatch threads or explicitly discard the processor. User code can be written in a familiar program style, including call blocking APIs, without starving other threads. However, kernel threads can force a context switch between threads at any time, thereby exposing race hazards, concurrency errors, or they will be located in a dive. In SMP systems, this is further exacerbated because kernel threads can be executed in parallel on different processors literally.

Practical Multi-Threading
The standardized interface for threading is a POSIX thread (in pthreads), which is a call to a set of C function libraries. Operating system vendors are free to implement interfaces as needed, but application developers should be able to use the same interface across multiple platforms. Most UNIX platforms, including Linux support Pthreads. Microsoft Windows has its own set of thread functions on the Process.h interface of multi-threading, just like Beginthread. Java provides the java.util.concurrent of a Java concurrency library that uses another standardized interface of the host operating system.


Multi-line libraries provides a function to create a new thread, which requires a function as a parameter. At the same time a thread is created and then it starts to run the passed function when the function returns to the end. The line libraries also provides synchronization capabilities, which enable it to achieve competitive conditions-errors using free multithreading features mutexes, key parts of condition variables, semaphores, monitors and other synchronization primitives.


Another pattern that a thread uses is that the thread pool, where it is started, then waits for the assigned task to create a set of threads. When a new task arrives, it wakes up, completes the task and goes back to waiting. This avoids the relatively expensive thread creation and destruction functions performed by each task and requires the thread to manage the hands of the application developer and leave it to the library or operating system, which is more suitable for optimizing thread management. For example, frameworks such as large central dispatch and threading building blocks.


In a programming model such as CUDA designed for data parallel computing, the line array runs the same code using only its ID to find its in-memory data parallelism. In essence, applications must be designed so that each thread performs the same actions on different segments of the memory, enabling them to operate in parallel and use the GPU architecture.


Next, let's see how to create a process that ends the process and threads waiting

First, create the process:

Let's start by understanding a function:


In order to write a program, this program is to create a thread that makes its output hhhhhhh, while the main program outputs Hello


Let's take a look at the results:


It seems that we have implemented the above mentioned function (that is, create a thread to make it output a certain character);

So how do we end it?

Is that okay?


Look at the results


It is indeed over, then return to exit?

Or, turn the program into this.




The results found that the process exited.




So, finally, let's look at how to wait for a process


Next, we use a code to implement:


Look at the results



This blog theory part of the study of Wikipedia, the implementation of the code part of the simple implementation of the thread creation, termination, and wait, as for other mutual exclusion and synchronization problems, will be introduced in detail later, thank you



Threading (Code implementation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.