Last active
December 15, 2017 03:17
-
-
Save rfdickerson/5fc83e25013ca16b223b483c171f4450 to your computer and use it in GitHub Desktop.
Example of Forking a thread in Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
func sayHello2() { | |
//sleep(5) | |
print("Hello!") | |
} | |
#if false | |
public func pthread_create( | |
_: UnsafeMutablePointer<pthread_t?>!, | |
_: UnsafePointer<pthread_attr_t>?, | |
_: @convention(c) (UnsafeMutablePointer<Swift.Void>) -> UnsafeMutablePointer<Swift.Void>?, | |
_: UnsafeMutablePointer<Swift.Void>? | |
) -> Int32 | |
#endif | |
func sayNumber(x: Int) { | |
} | |
func transform <S, T> (f: (S)->T) | |
-> (UnsafeMutablePointer<S>) -> UnsafeMutablePointer<T>? | |
{ | |
return { | |
( u: UnsafeMutablePointer<S>) -> UnsafeMutablePointer<T>? in | |
let r = UnsafeMutablePointer<T>(allocatingCapacity: 1) //leak? | |
r.pointee = f(u.pointee) | |
return r | |
} | |
} | |
func createThread1() { | |
let numCPU = sysconf(_SC_NPROCESSORS_ONLN) | |
print("You have \(numCPU) cores") | |
var t = pthread_t(nil) | |
let a = 5 | |
// pthread_create(&t, nil, sayHello, nil) | |
let x = transform(f: sayHello2) | |
pthread_create(&t, nil, | |
{ _ in sayHello2(); return nil }, | |
nil) | |
// pthread_create(&t, nil, sayNumber, &a) | |
let ep = UnsafeMutablePointer< | |
UnsafeMutablePointer<Swift.Void>? | |
>(allocatingCapacity: 1) | |
pthread_join(t!, ep) | |
print("ep", ep.pointee) | |
} | |
createThread1() | |
struct Argument { | |
var x = 1 | |
var y = "asdf" | |
var z = [1,2,3] | |
} | |
struct Result { | |
var a = 1 | |
var b = "asdf" | |
var c = [1,2,3] | |
} | |
func processArgumentToResult(_ s: Argument) -> Result { | |
print("returnS", s.x, s.y, s.z) | |
return Result(a: 12, b: "sss", c: [456]) | |
} | |
func createThread2() { | |
let numCPU = sysconf(_SC_NPROCESSORS_ONLN) | |
print("You still have \(numCPU) cores") | |
var t = pthread_t(nil) | |
let arg = Argument() | |
let argPointer = UnsafeMutablePointer<Argument>(allocatingCapacity: 1) | |
argPointer.initialize(with: arg) | |
pthread_create( | |
&t, | |
nil, | |
{ | |
let result = processArgumentToResult(UnsafeMutablePointer<Argument>($0).pointee) | |
let resultPointer = UnsafeMutablePointer<Result>(allocatingCapacity: 1) | |
resultPointer.initialize(with: result) | |
return UnsafeMutablePointer<Void>(resultPointer) | |
}, | |
argPointer) | |
let resultPointerPointer = UnsafeMutablePointer< | |
UnsafeMutablePointer<Void>? | |
>(allocatingCapacity: 1) | |
pthread_join(t!, resultPointerPointer) | |
let resultPointer = UnsafeMutablePointer<Result>(resultPointerPointer.pointee!) | |
print("resultPointer contents", resultPointer.pointee) | |
} | |
createThread2() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment