“线程安全”是什么意思,码? [英] What is meant by "thread-safe" code?

查看:162
本文介绍了“线程安全”是什么意思,码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是否意味着两个线程不能同时更改底层数据?或者是否意味着当多个线程运行时,给定的代码段将以可预测的结果运行?

解决方案

from wikipedia :



线程安全是一种适用于多线程程序环境的计算机编程概念。一段代码是线程安全的,如果它在多个线程的同时执行期间正确地运行。特别地,它必须满足多个线程访问相同共享数据的需要,并且需要在任何给定时间只有一个线程访问共享的数据片段。



...



有几种方法可以实现线程安全:

b

入侵



以这样的方式编写代码,任务,由另一个任务重新进入,然后从原始任务恢复。这需要在每个任务的本地变量中保存状态信息,通常在堆栈中,而不是在静态或全局变量中。



排除



使用确保只有一个线程可以随时读取或写入共享数据的机制,对共享数据进行序列化。如果一段代码访问多个共享的数据,需要非常小心 - 问题包括种族条件,死锁,活锁,饥饿和许多操作系统教科书中列举的各种其他问题。



线程本地存储



本地化变量,使每个线程都有自己的私有副本。这些变量在子程序和其他代码边界上保留其值,并且是线程安全的,因为它们是每个线程的本地,即使访问它们的代码可以重入。



原子操作



通过使用不能被其他线程中断的原子操作来访问共享数据。这通常需要使用特殊的机器语言指令,这可能在运行时库中可用。由于操作是原子操作,共享数据总是保持在有效状态,无论其他线程访问它。原子操作是许多线程锁定机制的基础。



阅读更多:



http://en.wikipedia.org/wiki/Thread_safety p>





Does it mean that two threads can't change the underlying data simultaneously? Or does it mean that the given code segment will run with predictable results when more than one thread are running it?

解决方案

from wikipedia :

Thread safety is a computer programming concept applicable in the context of multi-threaded programs. A piece of code is thread-safe if it functions correctly during simultaneous execution by multiple threads. In particular, it must satisfy the need for multiple threads to access the same shared data, and the need for a shared piece of data to be accessed by only one thread at any given time.

...

There are a few ways to achieve thread safety:

Re-entrancy

Writing code in such a way that it can be partially executed by one task, reentered by another task, and then resumed from the original task. This requires the saving of state information in variables local to each task, usually on its stack, instead of in static or global variables.

Mutual exclusion

Access to shared data is serialized using mechanisms that ensure only one thread reads or writes the shared data at any time. Great care is required if a piece of code accesses multiple shared pieces of data—problems include race conditions, deadlocks, livelocks, starvation, and various other ills enumerated in many operating systems textbooks.

Thread-local storage

Variables are localized so that each thread has its own private copy. These variables retain their values across subroutine and other code boundaries, and are thread-safe since they are local to each thread, even though the code which accesses them might be reentrant.

Atomic operations

Shared data are accessed by using atomic operations which cannot be interrupted by other threads. This usually requires using special machine language instructions, which might be available in a runtime library. Since the operations are atomic, the shared data are always kept in a valid state, no matter what other threads access it. Atomic operations form the basis of many thread locking mechanisms.

read more :

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


这篇关于“线程安全”是什么意思,码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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