具有“原子”读/写操作的Delphi数据类型列表? [英] List of Delphi data types with 'atomic' read/write operations?

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

问题描述

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



将所有数据类型的列表从任何线程和另一个列表也可以安全地写入任何线程,而无需诉诸各种同步方法。

解决方案

请注意,您可以基本上做所有事情delphi unthreadsafe而其他人则提到布尔型的对齐问题,这样可以隐藏真正的问题。



是的,您可以在任何线程中读取布尔值,并写入任何线程中的布尔值if它正确对齐。但是从布尔值中读取更改不一定是线程安全。假设你有一个布尔值,当你更新一个数字,以便另一个线程读取数字,你设置为true。

 如果NumberUpdated那么
begin
LocalNumber = TheNumber;
结束

由于优化处理器可以在NumberUpdated被读取之前读取TheNumber,因此您可能会获得旧的您的代码可能会变成:

 



code> temp = TheNumber;
如果NumberUpdated
begin
LocalNumber = temp;
结束

基本的经验法则:

读取是线程安全的写入不是线程安全的。

所以,如果你要写一个写保护数据,同步 everywhere ,你可以读取值,而写入可以可能会发生

另一方面,如果您只在一个线程中读取和写入值,那么它的线程是安全的。所以你可以在一个临时的位置做一大笔写作,然后同步更新应用范围的数据。



奖金blurb:



VCL不是线程安全的。在主线程中保留所有的ui内容的修改。在主线程中保持所有ui的创建。



许多函数也不是线程安全的,而其他函数通常取决于基础的winapi调用。 / p>

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


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.

解决方案

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.

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;

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.

Bonus blurb:

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.

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