All Projects → nvzqz → Threadly

nvzqz / Threadly

Licence: other
Type-safe thread-local storage in Swift

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Threadly

Fontblaster
Programmatically load custom fonts into your iOS and tvOS app.
Stars: ✭ 1,000 (+1624.14%)
Mutual labels:  tvos, cocoapods, carthage
Googlereporter
Easily integrate with Google Analytics in your iOS app
Stars: ✭ 479 (+725.86%)
Mutual labels:  tvos, cocoapods, carthage
Functionkit
A framework for functional types and operations designed to fit naturally into Swift.
Stars: ✭ 302 (+420.69%)
Mutual labels:  tvos, cocoapods, carthage
Swiftysound
SwiftySound is a simple library that lets you play sounds with a single line of code.
Stars: ✭ 995 (+1615.52%)
Mutual labels:  tvos, cocoapods, carthage
Sablurimageview
You can use blur effect and it's animation easily to call only two methods.
Stars: ✭ 538 (+827.59%)
Mutual labels:  tvos, cocoapods, carthage
Web3.swift
A pure swift Ethereum Web3 library
Stars: ✭ 295 (+408.62%)
Mutual labels:  tvos, cocoapods, carthage
Xcglogger
A debug log framework for use in Swift projects. Allows you to log details to the console (and optionally a file), just like you would have with NSLog() or print(), but with additional information, such as the date, function name, filename and line number.
Stars: ✭ 3,710 (+6296.55%)
Mutual labels:  tvos, cocoapods, carthage
Datez
📆 Breeze through Date, DateComponents, and TimeInterval with Swift!
Stars: ✭ 254 (+337.93%)
Mutual labels:  tvos, cocoapods, carthage
Swiftframeworktemplate
A template for new Swift iOS / macOS / tvOS / watchOS Framework project ready with travis-ci, cocoapods, Carthage, SwiftPM and a Readme file
Stars: ✭ 527 (+808.62%)
Mutual labels:  tvos, cocoapods, carthage
Anim
Swift animation library for iOS, tvOS and macOS.
Stars: ✭ 520 (+796.55%)
Mutual labels:  tvos, cocoapods, carthage
Sica
🦌 Simple Interface Core Animation. Run type-safe animation sequencially or parallelly
Stars: ✭ 980 (+1589.66%)
Mutual labels:  tvos, cocoapods, carthage
Urlembeddedview
URLEmbeddedView automatically caches the object that is confirmed the Open Graph Protocol.
Stars: ✭ 633 (+991.38%)
Mutual labels:  tvos, cocoapods, carthage
Jgprogresshud
An elegant and simple progress HUD for iOS and tvOS, compatible with Swift and ObjC.
Stars: ✭ 3,110 (+5262.07%)
Mutual labels:  tvos, cocoapods, carthage
Centeredcollectionview
A lightweight UICollectionViewLayout that 'pages' and centers its cells 🎡 written in Swift
Stars: ✭ 965 (+1563.79%)
Mutual labels:  tvos, cocoapods, carthage
Audioindicatorbars
AIB indicates for your app users which audio is playing. Just like the Podcasts app.
Stars: ✭ 279 (+381.03%)
Mutual labels:  tvos, cocoapods, carthage
Misterfusion
MisterFusion is Swift DSL for AutoLayout. It is the extremely clear, but concise syntax, in addition, can be used in both Swift and Objective-C. Support Safe Area and Size Class.
Stars: ✭ 314 (+441.38%)
Mutual labels:  tvos, cocoapods, carthage
Amplitude Ios
Native iOS/tvOS/macOS SDK
Stars: ✭ 216 (+272.41%)
Mutual labels:  tvos, cocoapods, carthage
Dots
Lightweight Concurrent Networking Framework
Stars: ✭ 35 (-39.66%)
Mutual labels:  tvos, carthage, concurrency
Watchdoginspector
Shows your current framerate (fps) in the status bar of your iOS app
Stars: ✭ 497 (+756.9%)
Mutual labels:  thread, cocoapods, carthage
Gradientview
Easily use gradients in UIKit for iOS & tvOS
Stars: ✭ 610 (+951.72%)
Mutual labels:  tvos, cocoapods, carthage

Threadly

Threadly is a Swift µframework that allows for type-safe thread-local storage.

What is Thread-Local Storage?

Thread-local storage (TLS) lets you define a single variable that each thread has its own separate copy of. This is great for cases such as having a mutable global variable that can't be safely accessed by multiple threads.

One example of this is with random number generators. Each thread can have its own seeded generator that's mutated on a per-thread basis. While this may potentially use more memory, it's much faster than accessing a shared global variable through a mutex.

Build Status

Branch Status
master Build Status

Installation

Compatibility

  • Platforms:
    • macOS 10.9+
    • iOS 8.0+
    • watchOS 2.0+
    • tvOS 9.0+
    • Linux
  • Xcode 8.0+
  • Swift 3.0+

Install Using Swift Package Manager

The Swift Package Manager is a decentralized dependency manager for Swift.

  1. Add the project to your Package.swift.

    import PackageDescription
    
    let package = Package(
        name: "MyAwesomeProject",
        dependencies: [
            .Package(url: "https://github.com/nvzqz/Threadly.git",
                     majorVersion: 1)
        ]
    )
    
  2. Import the Threadly module.

    import Threadly
    

Install Using CocoaPods

CocoaPods is a centralized dependency manager for Objective-C and Swift. Go here to learn more.

  1. Add the project to your Podfile.

    use_frameworks!
    
    pod 'Threadly', '~> 2.0.1'
    

    If you want to be on the bleeding edge, replace the last line with:

    pod 'Threadly', :git => 'https://github.com/nvzqz/Threadly.git'
    
  2. Run pod install and open the .xcworkspace file to launch Xcode.

  3. Import the Threadly framework.

    import Threadly
    

Install Using Carthage

Carthage is a decentralized dependency manager for Objective-C and Swift.

  1. Add the project to your Cartfile.

    github "nvzqz/Threadly"
    
  2. Run carthage update and follow the additional steps in order to add Threadly to your project.

  3. Import the Threadly framework.

    import Threadly
    

Install Manually

Simply add Threadly.swift into your project.

Usage

Try it out for yourself! Download the repo and open 'Threadly.playground'.

Initialization

There are two ways to initialize a thread-local value. The value can be initialized lazily when retrieved (init(value:) & init(create:)) or at the call site (init(capturing:)).

Using init(value:) is an @autoclosure shorthand for init(create:). This means that the thread-local value is initialized once per thread.

import Foundation

let array = ThreadLocal(value: [1, 2, 3])

Thread.detachNewThread {
    // Allocates an array with 3 elements for this thread
    let arr = array.inner.value
    doStuff(with: arr)
}

// Allocates another array of 3 elements for the main thread
doStuff(with: array.inner.value)

When using init(capturing:), the thread-local value is initialized at the call site.

import Foundation

// The inner value gets allocated
let array = ThreadLocal(capturing: [1, 2, 3])

Thread.detachNewThread {
    // Retrieves a shallow copy of the initial value that can be used in this
    // thread. If the thread-local value gets mutated, a deep copy occurs to
    // retain value semantics.
    let arr = array.inner.value
    doStuff(with: arr)
}

// Same as the other thread, but now in the main thread
doStuff(with: array.inner.value)

Exclusivity

Each thread has exclusive access to its own local variable.

import Foundation

let num = ThreadLocal(value: 42)

Thread.detachNewThread {
    withUnsafePointer(to: &num.inner.value) { ptr in
        print(ptr) // 0x00007fa6f86074a0
    }
}

withUnsafePointer(to: &num.inner.value) { ptr in
    print(ptr) // 0x00007fa6f844c920
}

License

All source code for Threadly is released under the MIT License.

Assets for Threadly are released under the Creative Commons Attribution-ShareAlike 4.0 International License and can be found in the assets branch.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].