4 ms·
I learned something about Thread.MemoryBarrier(). I expected the following to be safe: Dim V(99) As Integer Parallel.For(0, 100, Sub(i)
by cm2187 10y ago
I learned something about Thread.MemoryBarrier().
I expected the following to be safe:
Dim V(99) As Integer
Parallel.For(0, 100, Sub(i)
V(i) = i
End Sub)
Dim Result = V.Sum
If I understand correctly it looks like I need to flush the memory before accessing V:
Dim V(99) As Integer
Parallel.For(0, 100, Sub(i)
V(i) = i
End Sub)
Threading.Thread.MemoryBarrier()
Dim Result = V.Sum
- Matthias247 10y agoThat's interesting, I had thought the first one to be safe since Parallel.For should already imply synchronization: The operation should only return once everything was done - and the waiting everything done part of it should act as a memory barrier.
- taspeotis 10y agoThe CLR has some implicit memory barriers that make most things "just work." Take a look at "[t]he following implicitly generate full fences" here: http://www.albahari.com/threading/part4.aspx http://www.albahari.com/threading/part4.aspx. Parallel.For would probably be covered by "[a]nything that relies on signaling." There's more info scattered about in some Stack Overflow answers. The content is useful, just unofficial. E.g. http://stackoverflow.com/a/6932271/242520 http://stackoverflow.com/a/6932271/242520, http://stackoverflow.com/a/681872/242520 http://stackoverflow.com/a/681872/242520