1
General / Re: Multithreading - synchronize or not?
« Last post by Martin_fr on Today at 01:08:18 pm »https://en.wikipedia.org/wiki/Memory_barrier
So between modifying the data in "aEven" and setting "EvenFlag" for the other thread you need a barrier. (write barrier).
After checking the flag, and before reading the data, you need a readbarrier.
Assuming ONLY ONE thread each:
You still need InterlockedIncrement for the Counter that is not protected by any flags.
So between modifying the data in "aEven" and setting "EvenFlag" for the other thread you need a barrier. (write barrier).
After checking the flag, and before reading the data, you need a readbarrier.
Assuming ONLY ONE thread each:
- if FlagEven = 0 then // this can be a dirty read, no problem
- begin
- // FlagEven:=1; // not needed
- ReadWriteBarrier; // Don't read the below data before FlagEven really is 0
- // Don't write it either....
- for k:=0 to cBigSize-1 do aEven[k]:=EvenVal;
- // If I am correct then the other thread won't access them due to FlagEven => so "inc" is ok, no InterlockedIncrement needed)
- inc(EvenVal);
- inc(CountOutData);
- WriteBarrier; // Don't write the below, before all the above has been written
- FlagEven:=2; //-- send data
- end;
You still need InterlockedIncrement for the Counter that is not protected by any flags.