简体   繁体   English

Delphi并发内存模型?

[英]Delphi concurrency memory model?

Is there anything like Java Memory Model in Delphi? 在Delphi中有类似Java内存模型的东西吗? To prevent misunderstandings: I mean nothing like "huge/large/small", but the things related to visibility of changes to other threads. 为了防止误解:我的意思是“巨大/大/小”,但与其他线程的变化可见性相关的事情。

I'd say the Delphi memory model matches the C++ memory model. 我会说Delphi内存模型与C ++内存模型匹配。 That is, the compiler is not aware of multiple processes or multiple threads and does not provide any special support for those scenarios. 也就是说,编译器不知道多个进程或多个线程,并且不为这些方案提供任何特殊支持。 See "What is the C++ memory model for concurrency?" 请参阅“并发的C ++内存模型是什么?”

The Delphi 32 bit compiler does perform optimizations such as invariant code motion and does emit instruction sequences designed to avoid stalling dual pipelines. Delphi 32位编译器确实执行优化,例如不变的代码运动,并发出旨在避免停止双管道的指令序列。 However, the Delphi compiler does not contain an instruction scheduler or peephole optimizer, so opportunities for instruction reordering are slim to none. 但是,Delphi编译器不包含指令调度程序或窥孔优化器,因此指令重新排序的机会很少。 Delphi optimizations occur on the AST / IR before instruction emit. 在指令发出之前,在AST / IR上发生Delphi优化。

Local variables may be enregistered, but any source code reference to a variable that requires a memory address (such as passing a local variable to a var param, or taking the address of a local var) will force the compiler to commit the enregistered value to a memory location prior to use of the address, or may force the compiler to completely abandon enregistering the variable at all. 可以注册本地变量,但是对需要内存地址的变量的任何源代码引用(例如将局部变量传递给var param,或者获取本地var的地址)将强制编译器将注册的值提交到在使用地址之前的存储器位置,或者可能迫使编译器完全放弃注册变量。

The Delphi 32 bit compiler is fairly conservative in its optimizations. Delphi 32位编译器的优化相当保守。 The biggest performance gains from optimizations are from enregistering variables and intermediate results, and from various loop induction tricks. 优化带来的最大性能提升来自注册变量和中间结果,以及各种循环感应技巧。

Operations on global symbols or symbols residing in global memory (such as object fields) are not enregistered. 不会注册驻留在全局内存中的全局符号或符号(例如对象字段)的操作。 There is no "volatile" modifier. 没有“volatile”修饰符。

The compiler codegen patterns rely on the x86 architecture rules that register-sized writes to global memory at aligned addresses are atomic. 编译器codegen模式依赖于x86体系结构规则,对齐地址的全局内存的寄存器大小写入是原子的。 Writing of large data, byte data, or at unaligned addresses may cross a cache line and require two separate write operations within the single write instruction. 写入大数据,字节数据或未对齐地址可能会跨越高速缓存行,并且在单个写入指令中需要两个单独的写入操作。 The Delphi compiler is (mostly) oblivious to this. Delphi编译器(大部分)都没有注意到这一点。

Regardless, if you are writing Delphi code that accesses shared memory from different threads, it is always your responsibility to decide what thread synchronization measures are appropriate to your situation and implement them. 无论如何,如果您正在编写从不同线程访问共享内存的Delphi代码,那么您始终有责任确定哪些线程同步措施适合您的情况并实施它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM