PC Review


Reply
Thread Tools Rate Thread

Multi-threading question

 
 
Curious
Guest
Posts: n/a
 
      11th Mar 2010
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?
 
Reply With Quote
 
 
 
 
Mike Lovell
Guest
Posts: n/a
 
      11th Mar 2010
"Curious" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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# Blog
http://www.gotinker.com

 
Reply With Quote
 
 
 
 
Curious
Guest
Posts: n/a
 
      11th Mar 2010
On Mar 10, 10:11*pm, "Mike Lovell" <(E-Mail Removed)> wrote:
> "Curious" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
>
> > 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?
 
Reply With Quote
 
Mike Lovell
Guest
Posts: n/a
 
      12th Mar 2010
>> > 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

 
Reply With Quote
 
Curious
Guest
Posts: n/a
 
      28th Mar 2010

> 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 coverit:http://www.gotinker.com/2010/03/11/l...lti-threading/
>
> --
> Mike
> GoTinker, C# Bloghttp://www.gotinker.com- Hide quoted text -
>

Thanks for the answer! I never came across any issue with "lock". I
was grilled in a technical interview about "lock". I told him that I
used lock to resolve the issue with memory corruption. He then asked
me if there was a better method to use than to use lock, and that got
me.
 
Reply With Quote
 
psycho
Guest
Posts: n/a
 
      30th Mar 2010
On Mar 28, 6:18*am, Curious <(E-Mail Removed)> wrote:
> > 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# Bloghttp://www.gotinker.com-Hide quoted text -

>
> Thanks for the answer! I never came across any issue with "lock". I
> was grilled in a technical interview about "lock". I told him that I
> used lock to resolve the issue with memory corruption. He then asked
> me if there was a better method to use than to use lock, and that got
> me.


Lock keyword is just a convinient way to use Monitor
Which means
lock(obj)
{
// do something here
}

is equivalent to

Monitor.Enter(obj);
// do something
Monitor.Exit(obj);
 
Reply With Quote
 
MarkusSchaber
Guest
Posts: n/a
 
      31st Mar 2010
On 30 Mrz., 12:11, psycho <(E-Mail Removed)> wrote:

> Lock keyword is just a convinient way to use Monitor
> Which means
> lock(obj)
> {
> * * // do something here
>
> }
>
> is equivalent to
>
> Monitor.Enter(obj);
> // do something
> Monitor.Exit(obj);


Not exactly - more like
Monitor.Enter(obj);
try {
// do something
} finally {
Monitor.Exit(obj);
}
But there are some special precautions, AFAIK, to handle async
exceptions which can appear between Monitor.Enter and try.
 
Reply With Quote
 
MarkusSchaber
Guest
Posts: n/a
 
      31st Mar 2010
On 31 Mrz., 11:33, MarkusSchaber <(E-Mail Removed)> wrote:

> But there are some special precautions, AFAIK, to handle async
> exceptions which can appear between Monitor.Enter and try.


YFYI: You can find more infos at
http://blogs.msdn.com/ericlippert/ar...o-not-mix.aspx

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
between question, question 1 is a 3, question 3 is a 1 Jose Cortez Microsoft Access 1 16th Jul 2009 07:45 AM
Perhaps and off topic question....but could use some help with video question.....I don't need codec help, just a general question. Bret Miller DIY PC 0 13th Oct 2006 12:23 AM
Registry question and a certificates question John C Windows XP Performance 0 23rd Apr 2004 01:58 PM
Question; strange things happening, please read inside(??] [2/2] - question.txt (1/1) exgi Windows XP Help 4 14th Apr 2004 12:39 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:42 AM.