码迷,mamicode.com
首页 > 其他好文 > 详细

thread safe

时间:2016-08-23 11:38:03      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:

computer science J. Glenn Brookshear with contributions from David T. SmithIndiana University of Pennsylvania Dennis Brylow Marquette University 11th Edition

https://en.wikipedia.org/wiki/Thread_safety

https://en.wikipedia.org/wiki/Process_(computing)

https://en.wikipedia.org/wiki/Thread_(computing)

https://zh.wikipedia.org/wik/线程

https://zh.wikipedia.org/wiki/行程

https://zh.wikipedia.org/wiki/线程安全

 
Chapter 3
Operating Systems
3.3
Coordinating the Machine’s Activities
In this section we consider how an operating system coordinates the execution of application software, utility software, and units within the operating system itself. We begin with the concept of a process.
The Concept of a Process
One of the most fundamental concepts of modern operating systems is the distinction between a program and the activity of executing a program. The former is a static set of directions, whereas the latter is a dynamic activity whose properties  change  as  time  progresses.  (This  distinction  is  analogous  to  a  piece  of sheet music, sitting inert in a book on the shelf, versus a musician performing that piece by taking actions that the sheet music describes.) The activity of executing  a  program  under  the  control  of  the  operating  system  is  known  as  a process.
Associated with a process is the current status of the activity, called the process  state.This  state  includes  the  current  position  in  the  program  being executed  (the  value  of  the  program  counter)  as  well  as  the  values  in  the  other CPU  registers  and  the  associated  memory  cells.  Roughly  speaking,  the  process state is a snapshot of the machine at a particular time. At different times during the execution of a program (at different times in a process) different snapshots (different process states) will be observed.
Unlike a musician, who normally tries to play only one musical piece at a time, typical time-sharing/multitasking computers are running many processes, all competing for the computer’s resources. It is the task of the operating system to  manage  these processes  so  that  each  process  has  the  resources  (peripheral devices,  space  in  main  memory,  access  to  files,  and  access  to  a  CPU)  that  it needs,  that  independent  processes  do  not  interfere  with  one  another,  and  that processes that need to exchange information are able to do so.

 //中国大陆 进程
港台 行程、处理程序
进程为曾经是分时系统的基本运作单位。在面向进程设计的系统(如早期的UNIX,Linux 2.4及更早的版本)中,进程是程序的基本执行实体;在面向线程设计的系统(如当代多数操作系统、Linux 2.6及更新的版本)中,进程本身不是基本运行单位,而是线程的容器。程序本身只是指令、数据及其组织形式的描述,进程才是程序(那些指令和数据)的真正运行实例。若干进程有可能与同一个程序相关系,且每个进程皆可以同步(循序)或异步(平行)的方式独立运行。现代计算机系统可在同一段时间内以进程的形式将多个程序加载到内存中,并借由时间共享(或称时分复用),以在一个处理器上表现出同时(平行性)运行的感觉。同样的,使用多线程技术(多线程即每一个线程都代表一个进程内的一个独立执行上下文)的操作系统或计算机架构,同样程序的平行线程,可在多CPU主机或网络上真正同时运行(在不同的CPU上)。

 

In computing, a process is an instance of a computer program that is being executed. It contains the program code and its current activity. Depending on the operating system (OS), a process may be made up of multiple threads of execution that execute instructionsconcurrently.[1][2]

A computer program is a passive collection of instructions, while a process is the actual execution of those instructions. Several processes may be associated with the same program; for example, opening up several instances of the same program often means more than one process is being executed.

Multitasking is a method to allow multiple processes to shareprocessors (CPUs) and other system resources. Each CPU executes a single task at a time. However, multitasking allows each processor to switch between tasks that are being executed without having to wait for each task to finish. Depending on the operating system implementation, switches could be performed when tasks perform input/output operations, when a task indicates that it can be switched, or on hardware interrupts.

A common form of multitasking is time-sharing. Time-sharing is a method to allow fast response for interactive user applications. In time-sharing systems, context switches are performed rapidly, which makes it seem like multiple processes are being executed simultaneously on the same processor. This seeming execution of multiple processes simultaneously is called concurrency.

For security and reliability, most modern operating systems prevent direct communication between independent processes, providing strictly mediated and controlled inter-process communication functionality.

 

In computer science, a thread of execution is the smallest sequence of programmed instructions that can be managed independently by a scheduler, which is typically a part of the operating system[1] .The implementation of threads and processes differs between operating systems, but in most cases a thread is a component of a process. Multiple threads can exist within one process, executing concurrently and sharing resources such as memory, while different processes do not share these resources. In particular, the threads of a process share its executable code and the values of its variables at any given time.

Systems with a single processor generally implement multithreading by time slicing: the central processing unit (CPU) switches between different software threads. This context switchinggenerally happens very often and rapidly enough that users perceive the threads or tasks as running in parallel. On a multiprocessor or multi-core system, multiple threads can execute inparallel, with every processor or core executing a separate thread simultaneously; on a processor or core with hardware threads, separate software threads can also be executed concurrently by separate hardware threads.

//操作系统能够进行运算调度的最小单位
进程中的实际运作单位
一个进程中可以并发多个线程,每条线程并行执行不同的任务
线程是独立调度和分派的基本单位
同一进程中的多条线程将共享该进程中的全部系统资源,如虚拟地址空间,文件描述符和信号处理等等。但同一进程中的多个线程有各自的调用栈(call stack),自己的寄存器环境(register context),自己的线程本地存储(thread-local storage)。

Thread safety is a computer programming concept applicable in the context of multithreaded programs. A piece of code is thread-safe if it manipulates shared data structures only in a manner that guarantees safe execution by multiple threads at the same time. There are various strategies for making thread-safe data structures.[1][2]

A program may execute code in several threads simultaneously in a shared address space where each of those threads has access to virtually all of the memory of every other thread. Thread safety is a property that allows code to run in multithreaded environments by re-establishing some of the correspondences between the actual flow of control and the text of the program, by means of synchronization.

//指某个函数函数库多线程环境中被调用时,能够正确地处理多个线程之间的共享变量,使程序功能正确完成。

一般来说,线程安全的函数应该为每个调用它的线程分配专门的空间,来储存需要单独保存的状态(如果需要的话),不依赖于“线程惯性”,把多个线程共享的变量正确对待(如,通知编译器该变量为“易失(volatile)”型,阻止其进行一些不恰当的优化),而且,线程安全的函数一般不应该修改全局对象。

很多C库代码(比如某些strtok的实现,它将“多次调用中需要保持不变的状态”储存在静态变量中,导致不恰当的共享)不是线程安全的,在多线程环境中调用这些函数时,要进行特别的预防措施,或者寻找别的替代方案。

thread safe

标签:

原文地址:http://www.cnblogs.com/yuanjiangw/p/5798394.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!