>> > Here's the question - I've created a program that has multiple
>> > threads. Some threads use the same file. In order to avoid memory
>> > corruption when multiple threads access the same file, I use a "lock"
>> > around that block of code so only a single thread can access the file
>> > at the same time.
>>
>> > Someone asked me if there is a better way to handle this than using
>> > "lock", I don't know what to say. Any suggestion?
>>
>> In a word, no.
>>
>> That's the best way to handle it
>>
>> --
>> Mike
>> GoTinker, C# Bloghttp://www.gotinker.com
>
> I agree with you. However, I heard about monitor and sephamore. Are
> they relevant or even better than "lock"?
>
> I also read some complicated stuff about "notify" and "wait". Are they
> useful?
Depends what you're doing but then, in a word again, no. Well I guess
that's harsh to say they are not useful. But I have never had to resort to
using them in any high performance multi-threaded application I've ever
made. However I have had to use lock correctly and carefully controlled my
threads.
I'd bet you 25c you're not going to need them for your application.
Out of interest are you coming across some kind of issue with locking or
performance which has caused you to question 'lock'? Or was it just an
academic question?
For some reason 'lock' has come up a lot recently! I thought I'd cover it:
http://www.gotinker.com/2010/03/11/l...lti-threading/
--
Mike
GoTinker, C# Blog
http://www.gotinker.com