Giter VIP home page Giter VIP logo

swiftsynchronized's Introduction

SwiftSynchronized

SwiftSynchronized provides synchronized, a global public function that serves as a Swift-substitute for Objective-C's @synchronized directive. It also adds a performAndWait extension to NSLock and NSRecursiveLock.

Usage: Executing a critical section of code

Using synchronized:

synchronized(self) {
    // Critical section
}

Using NSLock and NSRecursiveLock:

lock.performAndWait {
    // Critical section
}

Usage: Returning/setting value

Using synchronized:

var _privateStorage: String // The actual private storage

var threadSafeAccessor: String { // A thread safe accessor
    get { return synchronized(self, _privateStorage) }
    set { synchronized(self, _privateStorage = newValue) }
}

Using NSLock and NSRecursiveLock:

let lock = NSLock() // Or NSRecursiveLock()
var _privateStorage: String // The actual private storage

var threadSafeAccessor: String { // A thread safe accessor
    get { return lock.performAndWait(_privateStorage) }
    set { lock.performAndWait(_privateStorage = newValue) }
}

Installation

To install via CocoaPods:

pod 'SwiftSynchronized'

Don't forget to:

import SwiftSynchronized

somewhere in your project.

You can also use Carthage, or simply add SwiftSynchronized.swift directly to your project.

Caution

Unlike Objective-C's @synchronized, Swift synchronized does not handle exceptions.

Changes

1.0.0 -- Added support for autoclosures which allows improved syntax for return values and extension for NSLock and NSRecursiveLock. Also added Carthage support and support for multiple platforms in CocoaPods. 0.0.1 -- Initial release

swiftsynchronized's People

Contributors

jedlewison avatar readmecritic avatar

Watchers

James Cloos avatar  avatar  avatar

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.