Results 1 to 10 of 10

Thread: Arrays and Dictionaries accesible from different threads??

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2018
    Posts
    42

    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    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
    38,989

    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

  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2018
    Posts
    42

    Re: Arrays and Dictionaries accesible from different threads??

    Hi everyone,

    Firstly I apologyze for my lately answer, the truth is I've been coding and finding out things by myself. I come to tell the results, so it can help some others in my situation:

    DICTIONARY

    I started using dictionaries but it gave me problem when handling them in parallel. The problem was simply that some of the writes was not performed and I found the dictionary had empty keys where it was supposed to be a key-value.

    IMPORTANT: It happened despite every thread uses its own set of keys and one thread never interfere with the dataset of other one. I suppose when dictionary is writing two different values just in the same moment, one of them is not stored.

    SOLUTION: Using ConcurrentDictionary instead of Dictionary. The usage is very similar and once you understand how to use it it's as easy as dictionary.

    PERFORMANCE: I've not perform tests comparing the same operations with dictionary and ConcurrentDictionary in one thread, mainly becouse I'm forced to use the second one (Concurrent). But I've to say I've gain a good amount of speed from dictionary using one thread to ConcurrentDictionary using a 4 core/4 threads 6600k@4,5Ghz (around 60% speed gain or even some more). I think there is not a noticeable performance lost in one thread use using Concurrent instead of normal Dictionary

    ARRAYS

    I've look for information about a.... ConcurrentArray? but I've not found any reference to this. To the moment I'm using the normal arrays, of course always reading never writing in multithread usage and I've not found any problem. Not to say if I find any inconvenience I'll come here to tell.

    Regards and many many thanks for the help, it's really valuable for me.
    Last edited by TassadarNET; Apr 29th, 2019 at 04:38 AM.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Arrays and Dictionaries accesible from different threads??

    No such thing as a ParallelDictionary, as far as I'm aware. Do you mean a ConcurrentDictionary? Array support is built into the language so they can't really add a ConcurrentArray because it wouldn't really mimic the way arrays work. There is a ConcurrentBag class, which is sort of like a thread-safe version of the List class, which is effectively a dynamic array. That said, ConcurrentBag doesn't implement IList, so you can't get and set items by index. You could implement your own thread-safe list by inheriting Collection(Of T) and using SyncLock internally.

  7. #7

    Thread Starter
    Member
    Join Date
    Jul 2018
    Posts
    42

    Re: Arrays and Dictionaries accesible from different threads??

    Sorry xD

    Yes, I meant ConcurrentDictionary

    I really have my needs covered with the classes I've built using ConcurrentDictionaryes. But for fixed size elements that are read very oftenly I supposed Arrays could be faster so I've create other classes/objets having Arrays as main data holder. I'll continue using them as I had in mind and if I detect problems related to concurrent access I'll post here

    Regards

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Arrays and Dictionaries accesible from different threads??

    Dictionaries are faster than arrays IF you know the key. If you have to iterate through the keys, then dictionaries are not faster. If you are accessing every member of the dictionary in a loop, then it is also not faster. However, if you want to get one item by key, then a dictionary is faster than iterating through the array looking for the item.
    My usual boring signature: Nothing

  9. #9
    New Member
    Join Date
    May 2019
    Posts
    1

    Re: Arrays and Dictionaries accesible from different threads??

    Quote Originally Posted by kfcSmitty View Post
    Unfortunately singletons are not inherently thread safe so you'll run into the same issues.
    Not true, singleton must always have one instance of the class. If not than it's not implemented correctly. For multi thread application singleton must use lucks (lazy loading) or static read only variable/property (eager loading) which guaranties thread safety.
    Last edited by arman-g; May 15th, 2019 at 02:51 PM.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Arrays and Dictionaries accesible from different threads??

    Quote Originally Posted by arman-g View Post
    Not true, singleton must always have one instance of the class. If not than it's not implemented correctly. For multi thread application singleton must use lucks (lazy loading) or static read only variable/property (eager loading) which guaranties thread safety.
    If singletons MUST do something in order to be thread-safe then they are not inherently thread-safe, therefore the statement you claim is not true actually is true.

Posting Permissions

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



Click Here to Expand Forum to Full Width