thread safe in Dispose method

This is a discussion on thread safe in Dispose method within the C# Programming forums, part of the General Programming Boards category; Hello everyone, Here is my code for Dispose method. Currently, I am thinking whether it is necessary to make it ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    thread safe in Dispose method

    Hello everyone,


    Here is my code for Dispose method. Currently, I am thinking whether it is necessary to make it thread safe by adding a lock to prevent two threads to execute Dispose methods at the same time?

    Notes:

    1. disposing is used to indicate explicit call to Dispose or implicit call from Finalizer;

    2. disposed is used to indicate whether current instance is Disposed or not, to prevent from disposing again. The thread safety consideration is used to protect this variable.

    Code:
            protected virtual void Dispose(bool disposing)
            {
                lock (DisposeLock)
                {
                    if ((false == disposed) && (true == disposing))
                    {
    	  // some operations here
                        disposed = true;
                    }
                }
            }

    thanks in advance,
    George

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The kind of object that needs disposal shouldn't be shared between threads in the first place.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    Quote Originally Posted by CornedBee View Post
    The kind of object that needs disposal shouldn't be shared between threads in the first place.
    If the object is shared between two threads, I need to ensure tw threads are not Disposing at the same time. Any ideas about how to make it thread safety? My concern is, if using any lock, when called from Finalier, the lock object may not be still available and may be Finalized before Dispose. Any ideas?


    regards,
    George

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I'm saying that the object shouldn't be shared. What kind of disposable object do you have that you want to share between threads?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thread Prog in C language (seg fault)
    By kumars in forum C Programming
    Replies: 22
    Last Post: 10-09-2008, 01:17 PM
  2. user thread library
    By Eran in forum C Programming
    Replies: 4
    Last Post: 06-17-2008, 01:44 AM
  3. Messaging Between Threads, Exiting Thread On Demand, Etc.
    By HyperShadow in forum C++ Programming
    Replies: 10
    Last Post: 06-09-2007, 01:04 PM
  4. C++ Threading?
    By draggy in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2005, 12:16 PM
  5. [code] Win32 Thread Object
    By Codeplug in forum Windows Programming
    Replies: 0
    Last Post: 06-03-2005, 03:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21