Code Monkey home page Code Monkey logo

thread-safe-dictionary's Introduction

Thread-Safe-Dictionary

Why do we need this?

  1. Dictionaries in Swift are not thread safe , they lead to wierd crashes which are very hard to debug.This class solves this problem by using a dictionary whose accesses are made thread safe by using a concurrent queue with a barrier.
  2. It also conforms to the Collection Protocol and also has all the relevant subscript overrides which means you can use it just like a normal dictionary.
  3. The key now conforms to Hashable so now ThreadSafeDictionary can be used with any key that conforms to the Hashable protocol.

Usage:

1. Normal Dictionary Usage:

let dict = [String: A]()
let value = dict["hello"]
print(value)

2. ThreadSafeDictionary Usage

let dict = ThreadSafeDictionary<String,A>()
let value = dict["hello"]
print(value)

where A is any custom class

you can even do a for each like a normal dictionary

dict.forEach { (key, value) in
            // your handling
}

So just by a single line change , you can enjoy benefits of thread safety and a swift implementation of a dictionary. Guys this is being actively tested and used , will push commits if i face any issues , feel free to post issues if you find any. Feels good to be back after a while

thread-safe-dictionary's People

Contributors

ithink32 avatar mejiagarcia avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

thread-safe-dictionary's Issues

Run time error

I am getting a run time error with this class. Can't replicate it but a lot of users are getting it and it is specifically on this line:

 var endIndex: Dictionary<V, T>.Index {
        self.concurrentQueue.sync {
            return self.dictionary.endIndex
        }
    }

removeValue and removeAll are async, which breaks the assumption of Dictionary

The implementation of removeValue and removeAll uses self.concurrentQueue.async, which results in a different behavior vs. the regular dictionary. This means it's not a fully drop-in replacement.

If a caller removes a value and then that caller (or a caller of the caller) checks the dictionary, it may still contain the value (for an indeterminate amount of time).

May I ask why these calls must be async?
Thanks.

Fatal error: Can't compare indices belonging to different collections

I am getting a strange runtime error after changing the aisShips Dictionary to the ThreadSafeDictionary:

Can't compare indices belonging to different collections

Following function is called by timer every 120 seconds. Every 20...30 minutes the app will crash with the above error. Error is caused by for (mmsi, aisShip) in aisShips
Given the time interval between crashes I suspect that the aisShips.removeValue(...) statement causes the trouble by removing a dictionary element from the dictionary. This was not an issue with the standard Dictionary.

`@objc func onAisPrune(_ timer: Timer)
{
let currentdate = Date()

    for (mmsi, aisShip) in aisShips
    {
        if currentdate.timeIntervalSince(aisShip.aisdate) > 20 * 60
        {
            nmea.dbLock.wait()
            aisShips.removeValue(forKey: mmsi)
            aisTracks.removeValue(forKey: mmsi)
            nmea.dbLock.signal()
            
            DispatchQueue.main.async {
                MapFactory.shared.removeAisTarget(mmsi: mmsi)
            }
            
        }
    }
}`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.