具有“线程安全"读/写操作的 Delphi 数据类型列表? [英] List of Delphi data types with 'thread-safe' read/write operations?

查看:31
本文介绍了具有“线程安全"读/写操作的 Delphi 数据类型列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

'boolean' 变量对于从任何线程读取和写入是线程安全的吗?我已经看到一些新闻组引用说它们是.是否还有其他数据类型可用?(枚举类型,也许是短整数?)

Are 'boolean' variables thread-safe for reading and writing from any thread? I've seen some newsgroup references to say that they are. Are any other data types available? (Enumerated types, short ints perhaps?)

最好有一个可以从任何线程安全读取的所有数据类型的列表,以及另一个也可以在任何线程中安全写入的列表,而不必求助于各种同步方法.

It would be nice to have a list of all data types that can be safely read from any thread and another list that can also be safely written to in any thread without having to resort to various synchronization methods.

推荐答案

请注意,您基本上可以使 delphi 中的所有内容都变为非线程安全的.虽然其他人提到布尔值的对齐问题,但这在某种程度上隐藏了真正的问题.

Please note that you can make essentially everything in delphi unthreadsafe. While others mention alignment problems on boolean this in a way hides the real problem.

是的,如果正确对齐,您可以在任何线程中读取布尔值并在任何线程中写入布尔值.但是无论如何,从您更改的布尔值中读取不一定是线程安全的".假设您有一个布尔值,当您更新一个数字时将其设置为 true,以便另一个线程读取该数字.

Yes, you can read a boolean in any thread and write to a boolean in any thread if it's correctly aligned. But reading from a boolean you change is not necessarily "thread safe" anyway. Say you have a boolean you set to true when you've updated a number so that another thread reads the number.

if NumberUpdated then
begin
  LocalNumber = TheNumber;
end;

由于处理器的优化,可以在读取 NumberUpdated 之前读取 TheNumber,因此即使您最后更新 NumberUpdated,您也可能获得 TheNumber 的旧值.

Due to optimizations the processor makes TheNumber may be read before NumberUpdated is read, thus you may get the old value of TheNumber eventhough you updated NumberUpdated last.

也就是,你的代码可能会变成:

Aka, your code may become:

temp = TheNumber;
if NumberUpdated the
begin
  LocalNumber = temp;
end;

恕我直言,一个基本的经验法则:
读取是线程安全的.写入不是线程安全的."
因此,如果您要通过同步随处对数据进行写保护,则您会读取该值,而写入可能可能发生.
另一方面,如果你只在一个线程中读写一个值,那么它就是线程安全的.因此,您可以在临时位置进行大量写入,然后同步应用程序范围内的数据更新.

Imho, a basic rule of thumb:
"Reads are thread safe. Writes are not thread safe."
So if you're going to do a write protect the data with synchronization everywhere you read the value while a write could potentially occur.
On the other hand, if you only read and write a value in one thread, then it's thread safe. So you can do a large chunk of writing in a temporary location, then synchronize an update of applicationwide data.

奖金简介:

VCL 不是线程安全的.将所有对 ui 内容的修改保留在主线程中.将所有 ui 内容的创建也保留在主线程中.

The VCL is not thread safe. Keep all modification of ui stuff in the main thread. Keep the creation of all ui stuff in the main thread too.

许多函数也不是线程安全的,而其他函数则是,它通常取决于底层的 winapi 调用.

Many functions are not thread safe either, while others are, it often depends on the underlying winapi calls.

我认为列表"不会有帮助,因为线程安全"可能意味着很多东西.

I don't think a "list" would be helpful as "thread safe" can mean a lot of stuff.

这篇关于具有“线程安全"读/写操作的 Delphi 数据类型列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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