为什么我不++单核机器上的线程安全的? [英] why is i++ not thread safe on a single core machine?

查看:158
本文介绍了为什么我不++单核机器上的线程安全的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/443500/are-incrementers-decrementers-var-var-etc-thread-safe\">Are增量器/递减器(VAR ++,var--)等线程安全的?

您能描述我在大会code级,为什么递增,从两个不同的线程的值不被视为安全的单核机器上?

Can you describe for me, at the assembly code level, why incrementing a value from two different threads is not considered safe on a single core machine?

推荐答案

考虑到可能会为一个语句生成像的说明我++ 。当然,这将取决于你的架构/指令集,但它可能会沿着线的东西:

Consider the instructions that might be generated for a statement like i++. Of course this will depend upon your architecture/instruction set, but it will probably be something along the lines of:

LOAD    @i, r0    ;load the value of 'i' into a register from memory
ADD     r0, 1     ;increment the value in the register
STORE   r0, @i    ;write the updated value back to memory

现在考虑如何多线程会在操作系统中实现了缝纫机多少个核心拥有。在最基本的层面上,OS将需要一些工具来中断当前线程的执行,保存其状态,并在不同的线程执行上下文切换。该OS具有知道哪个用户线程的内指示应作为一个原子操作来处理的任何自动方式,并具有启动任何两个指令之间的上下文切换的能力。

Now consider how multithreading would be implemented in an operating-system, regardless of how many cores the machine has. At the most basic level, the OS is going to need some facility to interrupt the execution of the current thread, save its state, and perform a context switch to a different thread. The OS has no automatic way of knowing which instructions inside of a user thread should be treated as an atomic operation, and has the ability to initiate a context switch between any two instructions.

因此​​,如果OS执行从一个线程到另一间的上下文切换时会发生什么 LOAD 添加?比方说, I 开始了一个0值,所以当第一个线程获得 R0 将被设置为0换出。操作系统将这个值保存为线程状态的一部分。现在第二个线程中运行,并执行相同的 LOAD 语句。在内存中值仍为0,那么 R0 得0装入一遍。线程递增值,将其写回内存,设置 I 为1的值,现在第一个线程继续执行,而操作系统恢复的值 R0 0作为其上下文切换的一部分。第一个线程现在执行的增量,设定 R0 1,和值1被存储在 I 一次。现在的价值 I 不正确,因为已经被应用两个增量,但该值只增加了1。

So what happens if the OS performs a context switch from one thread to the other between LOAD and ADD? Let's say that i started out with a value of 0, so r0 will be set to 0 when the first thread gets swapped out. The OS will save this value as part of that thread's state. Now the second thread runs, and performs the same LOAD statement. The value in memory is still 0, so r0 gets 0 loaded into it again. The thread increments the value and writes it back to memory, setting the value of i to 1. Now the first thread resumes execution, and the operating-system restores the value of r0 to 0 as part of its context switch. The first thread now performs its increment, setting r0 to 1, and the value of 1 gets stored in i again. Now the value of i is incorrect because two increments have been applied, but the value has only increased by 1.

所以,简单地说,尽管我++ 是一个高层次的语言一条语句,它生成多个汇编语言指令,这些指令将不会被处理由操作系统/运行环境的原子,除非你添加额外的同步逻辑身边。

So in a nutshell, even though i++ is a single statement in a high-level language, it generates multiple assembly-language instructions, and those instructions will not be treated as atomic by the operating-system/runtime environment unless you add extra synchronization logic around them.

这篇关于为什么我不++单核机器上的线程安全的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆