attribute your hasher with [ThreadStatic] ?  (and obviously move the init
check into the loop which would only be done once per thread)

http://msdn.microsoft.com/en-us/library/system.threadstaticattribute.aspx


On Fri, Feb 15, 2013 at 7:00 PM, Corneliu I. Tusnea
<[email protected]>wrote:

> I hit exactly the same issue with calculating hashes. I don't think it's
> threadsafe to do it. Just initialize it in each thread. I found the
> performance to be acceptable (at least for my use).
> However if you do find a better way I'd like to know :)
>
>
> On Fri, Feb 15, 2013 at 8:38 PM, mike smith <[email protected]> wrote:
>
>> SOme of the stuff around here about TLS?
>>
>> http://msdn.microsoft.com/en-us/library/6sby1byh.aspx
>>
>>
>> On Fri, Feb 15, 2013 at 7:25 PM, Greg Keogh <[email protected]> wrote:
>>
>>> Folks, I can't remember if I've asked about this before. I think I year
>>> ago when I hit this I gave up and worked around it ... but now I want it
>>> solved!
>>>
>>> I have a Parallel.ForEach loop over EnumerateFiles which is blazing
>>> fast, but then I added to code to make an MD5 hash of the files and it dies
>>> because of "Hash not valid for use in specified state". I have a single
>>> static MD5 hasher which lives for the life of the app, but calling it from
>>> many threads is not allowed.
>>>
>>> I have a suspicion there is an overload of Parallel.ForEach that allows
>>> each thread to start/use/finish its own resources. In theory I need each
>>> thread needs to make its own MD5 and dispose at the end. There are so many
>>> overloads in the Threading Tasks namespace that I'm confused at first and
>>> I'll have to plod through Jeffrey Richter's book overnight to find a
>>> possible example.
>>>
>>> Are there any Task ninjas out there who know how to do this sort of
>>> thing?
>>>
>>> Greg K
>>>
>>> P.S. I just went to pat the cat and on the way back I picked up
>>> Richter's CLR via C# and I found a skeleton example on page 742 where he
>>> adds up the sizes of all files in a folder. However, the example is very
>>> terse and I'll need to stare at it for a while to grok it. I will need to
>>> check that this example behaves consistently under all conditions such as
>>> cancellation, unhandled crash, etc. If I find anything useful I'll let you
>>> know.
>>>
>>
>>
>>
>> --
>> Meski
>>
>>    http://courteous.ly/aAOZcv
>>
>> "Going to Starbucks for coffee is like going to prison for sex. Sure,
>> you'll get it, but it's going to be rough" - Adam Hills
>>
>
>

Reply via email to