Poser une questionPoser une question
 

TraitéeHow to "Clone" a class?

  • vendredi 6 novembre 2009 18:08lxm Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    I have a class needs to be used in different thread simultaneously. So I do not want to create a semaphore for accessing it. Instead of duplicate this class in each thread, is there any way to clone it in each thread for easy maintenance of this class.

    Thnak you

Réponses

Toutes les réponses

  • vendredi 6 novembre 2009 18:13David M MortonMVP, ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée

    A few options:

    1. Use a struct.  Structs are inherently thread-safe because they copy themselves when you pass them to a method.
    2. Use a BinaryFormatter and MemoryStream to clone the object.  I have an example of such a method on this thread.
    3. Simply construct a new one before passing to the thread, which is obviously not the simplest solution depending on the complexity of the object you're dealing with.


    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
  • vendredi 6 novembre 2009 18:58lxm Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    pt 1 is the one I am looking for. How about the method in the struct? is it thread-safe as well? Here is the example, testSocket is the one need to be shared in different thread.

        public struct testSocket
        {
            private Socket _socket;

            private string ip;
            private int port;

            public testSocket(string addr, int p)
            {
                this.ip = addr;
                this.port = p;

                this._socket = null;
           
            }

            public void Func1()
            {
                this._socket = new Socket(...);

            }

            public void Send(byte[] b)
            {

                    this._socket.Send(b);
            }

            public void Receive(byte[] b)
            {

                    this._socket.Receive(b);
            }
    }

  • vendredi 6 novembre 2009 19:02David M MortonMVP, ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée
    The difficulty with this one would be the Socket class that resides within the struct. 

    The socket class could possibly be shared, but if all you're doing is setting the socket to null, you're fine.  I would suggest taking steps to ensure that you create a new socket every time before you use the socket, and that should solve it.  In other words, merge Func1() with Send. 

    Strings are immutable, so you should be safe there, and of course, int is just a value type itself, so that's good.

    If testSocket only sets _socket to null, and that's the only method called from a separate thread, you're good. 
    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
  • vendredi 6 novembre 2009 19:22lxm Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    The _socket is not null. in Func1(), after a new socket is created, I need to do something like :

                    IPEndPoint ipep = new IPEndPoint(this.ip, this.port);
                    _socket.BeginConnect(ipep, new AsyncCallback(Callback), _socket);

    would it be safe to call Func1() once in each thread, after _socket.Connected, do Send() and Receive() repeatly?






  • vendredi 6 novembre 2009 19:33David M MortonMVP, ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée
    Func1() is always safe.  It's reassigning the value.

    The ones that aren't safe are Send() and Receive(), as they might be operating on the same socket.  Say Thread1 calls Send() and Thread2() also calls send at the same time.  That's not good.  Only one thread should be calling Send() and Receive() unless you've first called Func1() on each thread every time. 

    Of course, you could implement the Clone() method, and simply create a new socket in the clone method using the same IP address and port.  That'd solve the issue, then you would continue using a class instead of a struct.
    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser