Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set realtime thread in Swift?

I'm trying to implement the set_realtime() function from this OSX scheduling guide in Swift.

I'm stuck on thread_policy_set(). The following causes this compiler error:

"Cannot specialize a non-generic definition"

I've also tried a couple other things but get various type mismatch errors with &policy. Any suggestions would be greatly appreciated! Thanks!

func set_realtime(period: UInt32, computation: UInt32, constraint: UInt32) -> Bool {
    let TIME_CONSTRAINT_POLICY: UInt32 = 2
    let TIME_CONSTRAINT_POLICY_COUNT = UInt32(MemoryLayout<thread_time_constraint_policy_data_t>.size / MemoryLayout<integer_t>.size)
    let SUCCESS: Int32 = 0
    var policy: thread_time_constraint_policy
    var ret: Int32
    let thread: thread_port_t = pthread_mach_thread_np(pthread_self())

    policy.period = period
    policy.computation = computation
    policy.constraint = constraint
    policy.preemptible = 1

    ret = withUnsafeMutablePointer<integer_t>(to: &policy, { ptr -> Int32 in
        thread_policy_set(thread, TIME_CONSTRAINT_POLICY, ptr, TIME_CONSTRAINT_POLICY_COUNT)
    })

    if ret != SUCCESS {
        print(stderr, "set_realtime() failed.\n")
        return false
    }
    return true
}
like image 355
Eggsalad Avatar asked Apr 11 '17 02:04

Eggsalad


People also ask

How many threads can be executed at a time in Swift?

However, if our application is running on a multicore (multi-CPU machine), then it is possible for two threads in your application to actually be executing simultaneously.

How do I start a thread in Swift?

Creating a thread in Swift is pretty simple using Thread class. You can either specify objc function through selector as a starting point, or pass a closure, and, more convenient way, subclass Thread . Thread is not started when the initializer is called. You need to call start() method explicitly to start the tread.

How does Swift manage multithreading?

All the tasks in the dispatch queue are executed either sequentially or concurrently. However, the dispatch queue always maintains the FIFO order of the tasks. This framework facilitates to execute code concurrently on the multi-core system by submitting a task to dispatch queues managed by the system.

What is multithreading in IOS Swift?

This delegation of tasks by the main thread to several other background threads is what we call multithreading. Multithreading is nothing but performing tasks concurrently, by scheduling them on multiple threads to improve the application's performance.


1 Answers

You cannot add a type placeholder to withUnsafeMutablePointer() to cast the pointer to a different type. You have to "rebind" the pointer instead:

ret = withUnsafeMutablePointer(to: &policy) {
    $0.withMemoryRebound(to: integer_t.self, capacity: Int(TIME_CONSTRAINT_POLICY_COUNT)) {
        thread_policy_set(thread, TIME_CONSTRAINT_POLICY, $0, TIME_CONSTRAINT_POLICY_COUNT)
    }
}
like image 184
Martin R Avatar answered Oct 27 '22 22:10

Martin R