Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gfx/swift-multithreading
A demo code for synchronization in Swift
https://github.com/gfx/swift-multithreading
Last synced: 26 days ago
JSON representation
A demo code for synchronization in Swift
- Host: GitHub
- URL: https://github.com/gfx/swift-multithreading
- Owner: gfx
- Created: 2014-07-23T13:38:41.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-11-23T01:44:21.000Z (almost 9 years ago)
- Last Synced: 2023-04-09T21:19:25.884Z (over 1 year ago)
- Language: Swift
- Homepage: http://qiita.com/gfx/items/532f9f975de5a965c58a
- Size: 17.6 KB
- Stars: 13
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Synchronization in Swift
Swift has no synchornozation functionality in the language,
but Foundation framework is available to synchronize the code.## Use of `dispatch_sync()`
```swift
class Foo {
var value = 0
let sync = dispatch_queue_create("\(self.dynamicType).sync", DISPATCH_QUEUE_SERIAL)func incrementWithSerialQueue() {
dispatch_sync(sync) {
self.value += 1
}
}```
## Use of `objc_sync_enter()` and `objc_sync_exit()`
```swift
class AutoSync {
let object : AnyObjectinit(_ obj : AnyObject) {
object = obj
objc_sync_enter(object)
}deinit {
objc_sync_exit(object)
}
}class Foo {
var value = 0func incrementWithObjcSync() {
let lock = AutoSync(self)
self.value += 1
}
}
```