RE: Thread.Monitor & references

From: Ansil MCAD (AnsilMCAD_at_discussions.microsoft.com)
Date: 10/05/04


Date: Mon, 4 Oct 2004 20:37:01 -0700

hi
you can either use lock block or Mutex itself
regards
ansil

"Brett Robichaud" wrote:

> I need to make access to a reference object threadsafe. My natural instinct
> was to simply use Monitor.Enter() and Exit(). The problem is that the
> object behind the reference changes frequently, so my understanding of
> Monitor indicates this would not protect my object. Example:
>
> Bitmap bmp = new Bitmap("x.jpg");
> Monitor.Enter(bmp);
> bmp = new Bitmap("y.jpg");
> Monitor.Exit(bmp);
>
> The above doesn't do when one might expect, correct? My C++ head says to me
> that what I really want to do is protect the 'pointer', not thing 'thing
> pointed to', since I have no other pointers to this thing. But I'm in C# now
> and my head is baffled.
>
> So currently I am using a Mutex to protect this kind of data, but it seems
> more klunky. Is there any way to use the slick Monitor (or lock) syntax and
> still protect this situation?
>
> -Brett-
>
>
>



Relevant Pages

  • Re: help w/ mutex
    ... Not sure I follow your code exactly, but a single thread can acquire a mutex *recursively* What they are designed to ... protect against is multiple threads accessing some resource at the same time. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: semaphore question
    ... You might modify global data, you might modify the internal state of a ... mutex that protects the object that code manipulates. ... How would you use a mutex or semaphore to protect something other than ... If there's conflicting data, there will be a problem whether or not ...
    (comp.os.linux.development.system)
  • Re: sysctl locking
    ... > 2) Extend the simple sysctl handler to use this mutex to protect the actual ... We must not hold the mutex during the useland copy in/out so ... This might need some extension for complex handler (i.e. ... If a sysctl needs that level of protection, it seems like they need to ...
    (freebsd-arch)
  • Re: sysctl locking
    ... > 2) Extend the simple sysctl handler to use this mutex to protect the actual ... We must not hold the mutex during the useland copy in/out so ... This might need some extension for complex handler (i.e. ... If a sysctl needs that level of protection, it seems like they need to ...
    (freebsd-current)