Is accessing the Thread object thread-safe?
If you design it right, then it should be thread-safe.
I mean, I could be writing the progress variable from inside the thread and reading it to update my progress bar from the main GUI thread at the same time.
For example, if I create a function 'GetProcess' which returns fileStream.position / Real(fileStream.Size), is it guaranteed that reading fileStream.position is thread safe e.g. what if I read from inside the thread at the same time and position is updated. I don't think updating fileStream.position would be an atomic operation by nature, unless TFileStream makes sure of it.
It would be thread-safe to call GetProgress from within the thread loop itself to update a variable. Simply retrieve the value using
InterLockedExchange or a similar approach. There is
an example of this here by ASerge. It is easy to expand it to get the overall progress if you have more than one thread, like one thread for sha1 and the other for sha256, or more than one file on a system with a few cores.
There is another method using
QueueAsyncCall as in
this example by Taazz.
As far as the caching goes, are you referring to caching the operating system would do? Because I can't find any caching code inside HashLib4Pascal's IHash.TransformStream.
Yes, the OS and the HD itself.
Additionally, since some hashing algorithms run much faster than others, I wonder how you could effectively cache the reads.
I doubt that you need to cache any reads in this specific case. To confirm, measure how long it takes to:
1-read one file without any processing.
2-process the data from memory.
Based on the results you can decide if you really benefit from caching.