为什么原子操作被认为是线程安全的? [英] Why are atomic operations considered thread-safe?

查看:171
本文介绍了为什么原子操作被认为是线程安全的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原子操作如何使线程安全?我已经在 Wikipedia上有关线程安全的文章中了解了该主题.但是这篇文章并没有真正解释幕后的过程.换句话说,为什么线程A所执行的原子"操作不能被线程B所中断?

How are atomic operations made thread-safe? I've read about the subject in Wikipedia's article on thread-safety. But the article didn't really explain the process behind the scenes. In other words, why can't an "atomic" operation executed by a thread A be interrupted by a thread B?

推荐答案

原子操作将完成或根本不完成.其他线程将无法看到正在进行中"的操作-永远不会在部分完成的状态下查看它.这就是原子"一词在此上下文中的意思.

An atomic operation will either be completed or not done at all. Other threads will not be able to see the operation "in progress" -- it will never be viewed in a partially complete state. This is what the word "atomic" means in this context.

实现真实性的幕后魔力因实现而异.出于并发设计的目的,您可以依靠的是执行时全有或全无的保证.

The behind-the-scenes magic for making that true will vary from implementation to implementation. For the purposes of your concurrency design, all you can rely on is that all-or-nothing guarantee on execution.

这篇关于为什么原子操作被认为是线程安全的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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