![]() InterlockedExchange / InterlockedIncrement functions are low-level asm opcodes with a LOCK prefix (or locked by design, like the XCHG EDX, opcode), which will indeed force the cache coherency for all CPU cores, and therefore make the asm opcode execution thread-safe.įor instance, here is how a string reference count is implemented when you assign a string value (see _LStrAsg in System.pas - this is from our optimized version of the RTL for Delphi 7/2002 - since Delphi original code is copyrighted): With multi-threading, you are not sure that the code you write, via individual instructions, is executed as expected, when it deals with shared variables. Since code expects the data to be coherent, some multi-thread programs may behave wrongly. ![]() Another issue which may lead into race condition is two threads writing to a resource at the same time: it's impossible to know which value will be stored afterward. The CPU per-core cache is just one of the possible issues, which will lead into reading wrong values. ![]() "protect your shared variables with locks (aka critical sections), because you are not sure that the data you read/write is the same for all threads".
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |