PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Arrays and Dictionaries accesible from different threads??-VBForums
Results 1 to 4 of 4

Thread: Arrays and Dictionaries accesible from different threads??

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2018
    Posts
    23

    Question Arrays and Dictionaries accesible from different threads??

    Hi everyone,

    I have create a class that contains basically a 2D array with data that are read from an excel sheet when program starts. This class also have some little dictionaries that holds indexs information, and of course I've methods to populate the array, create index of a column/columns and other things.

    Once created it works perfect and using the methods it's very easy and fast to take the functionality I need.

    My idea now is to create and static class with variables of this kind that will be globally accessible from everywhere, since those data are needed with a high frecuency. Notice those data are read-only.

    The problem comes when I want to create a multithreaded program using System.Threading.Tasks (Parallel.For and similar). According to what I've read those global variables created to access this dataset are not suitable for multithread.

    I also want to create a class to create other dataset type, but this time using dictionaries. I need those objects to be available for multithread access. In this case data will be read-write, but since every thread will access to a certain set of the data there will not be problem of data inconsistency becouse accessing in multithread.

    But again, I've read common dictionaryes are not suitable for multithread and I'll have problems.

    I've read something called "singleton", but I've nothing clear by now.

    Could someone gime me a link for someplace that explain how to do this in the best possible way?

    Many thanks in advance and regards.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    101,845

    Re: Arrays and Dictionaries accesible from different threads??

    A singleton is simply a class that has one and only one instance. That's it, that's all. To create one, do the following:

    1. Make the only constructor private.
    2. Create a static, read-only property that returns the one and only instance.
    3. In that property, check whether an instance exists and, if not, create one, then return that one and only instance.

    All access of that instance is then done via that property, e.g.
    vb.net Code:
    1. var someValue = SingletonClass.InstanceProperty.SomeProperty;

  3. #3
    March 2015 Winner kfcSmitty's Avatar
    Join Date
    May 2005
    Location
    Kingston, Ontario
    Posts
    2,152

    Re: Arrays and Dictionaries accesible from different threads??

    Unfortunately singletons are not inherently thread safe so you'll run into the same issues.

    You say you read the data from excel and use the dictionaries for indexes. Does that mean it is read-only? If so, then you don't have anything to worry about as reading the data won't cause any problems (assuming you've instantiated the dictionary before any of the other threads start grabbing the data).

    If you're looking to to just have a thread-safe dictionary, you could use a ConcurrentDictionary (http://dotnetpattern.com/csharp-concurrentdictionary). If you're looking to do more than just a basic dictionary, you'll probably want to look into using locks (https://docs.microsoft.com/en-us/dot...lock-statement) to ensure thread-safety when reading/writing to your indexes.

    If you want to use a singleton, you will need to implement locking both in the GetInstance method (which is what creates the single instance of the class) as well as for any of the accessors and mutators of any properties you may have within that class.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    33,452

    Re: Arrays and Dictionaries accesible from different threads??

    I'd say not to bother with the Singleton. It doesn't really suit all that well, in this case, or in any other, really. There's some debate about this, and I've found some cases where they seemed to make sense, but I usually ended up being wrong, in that the Singleton didn't provide any real benefit. It won't benefit you with threading, either. KFCSmitty covers it all pretty well. If all your data is read only, then threading isn't an issue.

    What people are likely referring to is the fact that very few data constructs are read only. If you only ever write to the structure from one thread, then you're safe. At worst, one thread is reading as the other thread is writing, so what is read is immediately stale, but it was still right at the instant of the read, and nobody can see the future. If you have multiple threads writing to a data construct, then you can get some truly strange results if you don't lock access to the structure. You could have writes that don't seem to happen, or seem to happen twice, and the pattern can be unpredictable and subject to change at any time. Reads don't have that issue, so you're safe with reading.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width