线程安全是什么意思? [英] What does threadsafe mean?

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

问题描述

最近,我尝试从线程(UI线程除外)访问文本框,并引发了异常.它说了一些有关代码不是线程安全的"的内容,因此我最终写了一个委托(MSDN的示例有所帮助)并调用了它.

Recently I tried to Access a textbox from a thread (other than the UI thread) and an exception was thrown. It said something about the "code not being thread safe" and so I ended up writing a delegate (sample from MSDN helped) and calling it instead.

但是即使如此,我还是不太明白为什么所有额外的代码都是必需的.

But even so I didn't quite understand why all the extra code was necessary.

更新: 如果我检查

Controls.CheckForIllegalCrossThread..blah =true

推荐答案

Eric Lippert 有一篇不错的博客文章标题为所谓的线程安全"是什么?关于维基百科中关于线程安全的定义.

Eric Lippert has a nice blog post entitled What is this thing you call "thread safe"? about the definition of thread safety as found of Wikipedia.

从链接中提取的3项重要内容:

3 important things extracted from the links :

如果一段代码在执行期间正常运行,则该代码是线程安全的 由多个线程同时执行."

"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."

绝对值得一读!

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

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