简体   繁体   中英

Delphi concurrency memory model?

Is there anything like Java Memory Model in Delphi? 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. 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?"

The Delphi 32 bit compiler does perform optimizations such as invariant code motion and does emit instruction sequences designed to avoid stalling dual pipelines. However, the Delphi compiler does not contain an instruction scheduler or peephole optimizer, so opportunities for instruction reordering are slim to none. Delphi optimizations occur on the AST / IR before instruction emit.

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.

The Delphi 32 bit compiler is fairly conservative in its optimizations. 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.

The compiler codegen patterns rely on the x86 architecture rules that register-sized writes to global memory at aligned addresses are atomic. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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