3 ms·
I'd go farther: don't try to read from memory at the same time that someone else is writing it. In the absence of explicit guarantees of atomicity, don't assu
by nkurz 17y ago
I'd go farther: don't try to read from memory at the same time that someone else is writing it. In the absence of explicit guarantees of atomicity, don't assume that any guarantees exist.
The interesting discovery in the article isn't that memcpy() can leave things in an intermediate state, but that releasing a JNI array triggers a copy even if the array is unaltered.
But this looks like a bug in the program (or the VM): either that copy back to the heap should not be occuring, or there should be some locking here. What if the data was changed? Would the Java thread just be expected to deal with all intermediate states? Alternatively, if the data is read-only, don't write to it!
- pmjordan 17y agoBut this looks like a bug in the program (or the VM): either that copy back to the heap should not be occuring, or there should be some locking here. What if the data was changed? Would the Java thread just be expected to deal with all intermediate states? Alternatively, if the data is read-only, don't write to it! In the comments to the post you can see that the correct way to prevent a copy back would be to release the memory using JNI_ABORT. As far as I know, there's no way to enforce immutability on array contents in Java, so it's an implicit contract in the system: the contents really never does change.