How can I avoid these variables to be shared between threads? RRS feed

  • Question

  • Hi,

    I am making a complex program that needs to be ran in pararell to get lower excecution time. The  problem is that the variables of the class "Tracker" (that is used in all the program) are being shared between threads and so I get problems with the results.

    I am new on threading and I am not sure about how can I avoid these variables to be shared, could you help me?

    I show the class:

    class Tracker
            //Tracker class
            //Basic characteristics
            public double[] northPost { get; set; } = new double[3]; //x,y,z coordinates of north post of the tracker 
            public double[] southPost { get; set; } = new double[3]; //x,y,z coordinates of south post of the tracker
            public double A { get; set; } //tracker width
            public double R { get; set; } //tracker pitch
            public double maxTilt { get; set; } //tilt limit
            public int Nx { get; set; } //cell divisions horizontal
            public int Ny { get; set; }//cell divisions vertical
            public List<double> azimut { get; set; }
            public List<double> elevation { get; set; }
            public List<double> SHFdirect { get; set; } //shadow factor on direct irradiance
            public List<double> SHFdiff { get; set; } //shadow factor on diffuse irradiance
            public List<double> SHFalb { get; set; } //shadow factor on albedo
            //Coordinates of tracker points (are going to be erased when not used)
            public double[,] cornerCoordinates; //corners coordintaes
            public double[,,] trackerCells; //each cell division coordinate (a cell is a division on the tacker to perform calculations)
            public double tilt; //tilt
            public double[] normal; //normal vector of tracker plane

    Wednesday, July 31, 2019 1:49 PM

All replies

  • The only time concurrent access is a problem is if threads are both reading and writing to the object.  For example, if you were to remove all of the "set;" clauses here, so that the data was read-only, then there is no chance for a problem to occur.  You can have 100 threads access the object at once.  From the names of the fields, maybe that is something you could think about doing.

    If the objects need to be updated, then you have a synchronization problem.  You can use the lock() statement to let a thread have exclusive access, but you must lock access to both writing and reading the fields that can be modified, and excessive use of "lock" will impact performance.

    When a thread modifies the object, will you need to notify the other threads?  How will you do that?  What action will they take?  Some languages (like ML) do not allow object to be modified, specifically so they can get massive multithreading.  When you need to update an object, you create a new one with the updated values.  Would that work for you?  Create a new object with new fields, then notify everyone else that a new object is available?

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Wednesday, July 31, 2019 6:41 PM
  • I am new on threading and I am not sure about how can I avoid these variables to be shared

    Well, if you want a new copy of each variable for each thread, the easiest way is to create a new instance of the class for each thread. Then, get the Thread to operate on its own instance. All of the variables in the instance will only be used by that thread.

    The difficult problem occurs when you do not want separate variables, but rather want several threads to access the same variable. That's when you have to use synchronization techniques. But for different variables, piece of cake: just use different instances of the class.

    Wednesday, July 31, 2019 8:29 PM
  • In my program each thread has to study a list of different objects "Tracker", these trackers are different on each thread, but the threads seems to be sharing the data from Tracker. Do you know any way to debug threads? Maybe I can find the problem. 
    Thursday, August 1, 2019 7:28 AM
  • Could I remove all of the "set;" and create a method set with a lock() inside in order to write in the variables safely?
    Thursday, August 1, 2019 7:39 AM
  • Do you know any way to debug threads?
    Yes. In Visual Studio place a breakpoint and start debugging. When it stops at the breakpoint, go to the Debug menu, then Windows -> Threads. In the Threads window you will see a list of your threads. You can double-click on any of them and the editor will take you to the point in the source code that shows what this thread was executing at the time that the breakpoint was hit. You can examine the variables, and you will see the values that this thread was seeing.
    Thursday, August 1, 2019 8:24 AM