 
 import Foundation
 
+fileprivate func synced(_ lock: AnyObject, closure: (() throws ->  Void)) rethrows {
+    objc_sync_enter(lock)
+    try closure()
+    objc_sync_exit(lock)
+}
+
 public struct LNZWeakCollection<T>: CustomStringConvertible, Sequence, IteratorProtocol {
     private var weakReferecesContainers: [LNZWeakContainer<AnyObject>] = [LNZWeakContainer<AnyObject>]()
     private var currentIndex: Int = 0
+    private let lockObject = NSObject()
     
     ///The count of nonnil objects stored in the array
     public var count: Int { return weakReferences.count }

    
    public mutating func next() -> T? {
        cleanup()
        guard count > 0 && currentIndex < count else { return nil }
        let object = weakReferences[currentIndex]
        currentIndex += 1
        return object
    }
}

     - parameter object: The object to be removed.
     */
    @discardableResult public mutating func remove(object: T)-> T? {
        var result: T?
        synced(lockObject) {
            guard let index = weakReferecesContainers.index(where: { $0.weakReference === object as AnyObject }) else { return }
            result = weakReferecesContainers.remove(at: index).weakReference as? T
        }
        
        return result
    }
    
    /**
