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

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


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 }
