Multi-threading question

Discussion in 'Microsoft Dot NET' started by Curious, Mar 11, 2010.

  1. Curious

    Curious Guest

    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?
     
    Curious, Mar 11, 2010
    #1
    1. Advertisements

  2. Curious

    Mike Lovell Guest

    "Curious" <> wrote in message
    news:...
    > 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
     
    Mike Lovell, Mar 11, 2010
    #2
    1. Advertisements

  3. Curious

    Curious Guest

    On Mar 10, 10:11 pm, "Mike Lovell" <> wrote:
    > "Curious" <> wrote in message
    >
    > news:...
    >
    > > 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?
     
    Curious, Mar 11, 2010
    #3
  4. Curious

    Mike Lovell Guest

    >> > 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/locking-and-multi-threading/

    --
    Mike
    GoTinker, C# Blog
    http://www.gotinker.com
     
    Mike Lovell, Mar 12, 2010
    #4
  5. Curious

    Curious Guest


    > 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/locking-and-multi-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.
     
    Curious, Mar 28, 2010
    #5
  6. Curious

    psycho Guest

    On Mar 28, 6:18 am, Curious <> 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/locking-and-multi-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);
     
    psycho, Mar 30, 2010
    #6
  7. On 30 Mrz., 12:11, psycho <> 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.
     
    MarkusSchaber, Mar 31, 2010
    #7
  8. MarkusSchaber, Mar 31, 2010
    #8
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alina

    disconnected DataSet multi-threading question

    Alina, Jul 16, 2003, in forum: Microsoft Dot NET
    Replies:
    0
    Views:
    565
    Alina
    Jul 16, 2003
  2. Guest

    Animation and multi-threading - NOT!

    Guest, Mar 12, 2004, in forum: Microsoft Dot NET
    Replies:
    1
    Views:
    281
    Rick Powell
    Mar 14, 2004
  3. Michael D Murphy

    Multi-Threading Question

    Michael D Murphy, Sep 29, 2004, in forum: Microsoft Dot NET
    Replies:
    7
    Views:
    153
    Jay B. Harlow [MVP - Outlook]
    Oct 4, 2004
  4. Curious

    Newbie question on multi-threading

    Curious, Mar 17, 2008, in forum: Microsoft Dot NET
    Replies:
    0
    Views:
    141
    Curious
    Mar 17, 2008
  5. Curious

    Newbie question on multi-threading

    Curious, Mar 17, 2008, in forum: Microsoft Dot NET
    Replies:
    5
    Views:
    130
    Curious
    Mar 17, 2008
Loading...

Share This Page