Unsynchronized int access from threads
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Fri Jun 19 00:43:28 UTC 2020
On 6/18/20 6:15 PM, H. S. Teoh wrote:
> On Thu, Jun 18, 2020 at 03:07:54PM -0400, Steven Schveighoffer via Digitalmars-d wrote:
> [...]
>> Let's say a value is 0xffff0000
>>
>> Thread that is looking for 0s reads the lower half of the number ->
>> 0x????0000
>>
>> Context switches, thread 2 writes 0x0000ffff into that memory slot.
>>
>> Back at the zero-scanning thread, it reads the second half ->
>> 0x0000????
>>
>> Combines with the first part, and now it sees a 0 where there isn't
>> one.
> [...]
>
> Hmmm very good point, hadn't thought of that. :-/ Oh well, I'll just
> think of another way of parallelizing my code. I think I can break the
> work up into large enough chunks that the overhead of using traditional
> synchronization primitives will be insignificant.
Once again the asinine response "all inter-thread communication must be
done by a handshake in which both threads participate knowingly" turns
out to be correct.
More information about the Digitalmars-d
mailing list