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

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

问题描述

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

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;

由于进行了优化,处理器使TheNumber可以在读取NumberUpdated之前被读取,因此即使最后一次更新了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,基本的经验法则:
读取是线程安全的.写入不是线程安全的."
因此,如果要通过同步无处不在进行写保护数据,则可以读取该值,而可能可能发生写操作.
另一方面,如果您仅在一个线程中读写值,那么它是线程安全的.因此,您可以在临时位置进行大量写操作,然后同步应用程序范围数据的更新.

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天全站免登陆