Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can threads safely read variables set by VCL events?

Is it safe for a thread to READ a variable set by a Delphi VCL event?

When a user clicks on a VCL TCheckbox, the main thread sets a boolean to the checkbox's Checked state.

CheckboxState := CheckBox1.Checked;

At any time, a thread reads that variable

if CheckBoxState then ...

It doesn't matter if the thread "misses" a change to the boolean, because the thread checks the variable in a loop as it does other things. So it will see the state change eventually...

Is this safe? Or do I need special code? Is surrounding the read and write of the variable (in the thread and main thread respectively) with critical code calls necessary and sufficient?

As I said, it doesn't matter if the thread gets the "wrong" value, but I keep thinking that there might be a low-level problem if one thread tries to read a variable while the main thread is in the middle of writing it, or vice versa.

My question is similar to this one: Cross thread reading of a variable who's value is not considered important.

(Also related to my previous question: Using EnterCriticalSection in Thread to update VCL label)

like image 311
RobertFrank Avatar asked Apr 08 '10 14:04

RobertFrank


People also ask

Is reading variables thread safe?

As long as it's a plain variable, it's no risk. If it is a property, reading it can possibly have side effects, so is not guaranteed to be thread safe. Save this answer.

Can threads share variables?

Threads can "share" variables in the initialized data, uninitialized data, and heap segments.

Are global variables thread safe python?

Local variables and parameters are always thread-safe. Instance variables, class variables, and global variables may not be thread-safe (but they might be).

Are class variables shared between threads?

Yes they are shared, so you have to handle exclusion with some primitives or if you can afford it use "synchronized" methods. Threads don't share anything by themselves, you have to make them share whatever it is fields/properties.


2 Answers

This is safe, for three reasons:

  • Only one thread writes to the variable.

  • The variable is only one byte, so there is no way to read an inconsistent value. It will be read either as True or as False. There can't be alignment issues with Delphi boolean values.

  • The Delphi compiler does no extensive checks whether a variable is actually written to, and does not "optimize" away any code if not. Non-local variables will always be read, there is no need for the volatile specifier.

Having said that, if you are really unsure about this you could use an integer value instead of the boolean, and use the InterlockedExchange() function to write to the variable. This is overkill here, but it's a good technique to know about, because for single machine word sized values it may eliminate the need for locks.

You can also replace the boolean by a proper synchronization primitive, like an event, and have the thread block on that - this would help you eliminate busy loops in the thread.

like image 186
mghie Avatar answered Oct 06 '22 01:10

mghie


In your case (Checked property) a read operation is atomic, so it is safe. That is the same as with TThread.Terminated property; the simple read and write operations for the properly aligned bytes, words and doublewords are atomic. You can check intel documentation for more information:

CHAPTER 8 - MULTIPLE-PROCESSOR MANAGEMENT

8.1.1 Guaranteed Atomic Operations

The Intel486 processor (and newer processors since) guarantees that the following basic memory operations will always be carried out atomically:

  • Reading or writing a byte
  • Reading or writing a word aligned on a 16-bit boundary
  • Reading or writing a doubleword aligned on a 32-bit boundary

The Pentium processor (and newer processors since) guarantees that the following additional memory operations will always be carried out atomically:

  • Reading or writing a quadword aligned on a 64-bit boundary
  • 16-bit accesses to uncached memory locations that fit within a 32-bit data bus
like image 43
kludg Avatar answered Oct 06 '22 01:10

kludg