How to Set Realtime Thread in Swift

How to set realtime thread in Swift?

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)
}
}

how to start a global thread in swift

Use Grand Central Dispatch

    let myQueue: dispatch_queue_t = dispatch_queue_create("com.example.queue", nil)
dispatch_async(myQueue, { () -> Void in
// Execute some code
})

Do I have place upload or download tasks onto a background thread or does Swift do it for me?

URLSession ALWAYS does network interactions on a background thread. You don't have to worry about that.

The completion handlers/delegate methods are also run on a background thread by default. That lets you do time-consuming processing on the resulting data without tying up the main thread.

Unless you point give your URL session a different delegate queue when you create it, you should wrap any UI code you put in your completion handlers/delegate methods in a call to the main thread.

Alternately, you can create a URLSession and give it a foreground queue in the delegateQueue parameter you pass in the initializer. If you do that then your completion handlers/delegate methods will be run on that foreground queue (and thus run on the main thread.) If you do that you don't need to explicitly wrap UIKit calls in a call to the main thread, but you will stall the UI if you do time-consuming work in your URLSession completion handlers/delegate methods

Understanding Threads Swift

Let me start this way. Unless you are writing a special kind of application (and you will know if you are), forget about threads. Working with threads is complex and tricky. Use dispatch queues… it's simpler and easier.

Dispatch queues run tasks. Tasks are closures (blocks) or functions. When you need to run a task off the main dispatch queue, you call one of the dispatch_ functions, the primary one being dispatch_async(). When you call dispatch_async(), you need to specify which queue to run the task on. To get a queue, you call one of the dispatch_queue_create() or dispatch_get_, the primary one being dispatch_get_global_queue.


NOTE: Swift 3 changed this from a function model to an object model. The dispatch_ functions are instance methods of DispatchQueue. The dispatch_get_ functions are turned into class methods/properties of DispatchQueue


// Swift 3
DispatchQueue.global(qos: .background).async {
var calculation = arc4random()
}

// Swift 2
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) {
var calculation = arc4random()
}

The trouble here is any and all tasks which update the UI must be run on the main thread. This is usually done by calling dispatch_async() on the main queue (dispatch_get_main_queue()).

// Swift 3
DispatchQueue.global(qos: .background).async {
var calculation = arc4random()

DispatchQueue.main.async {
print("\(calculation)")
}
}

// Swift 2
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) {
var calculation = arc4random()

dispatch_async(dispatch_get_main_queue()) {
print("\(calculation)")
}
}

The gory details are messy. To keep it simple, dispatch queues manage thread pools. It is up to the dispatch queue to create, run, and eventually dispose of threads. The main queue is a special queue which has only 1 thread. The operating system is tasked with assigning threads to a processor and executing the task running on the thread.


With all that out of the way, now I will answer your questions.

  • Is a single thread allocated to a piece of code until that code has completed?

A task will run in a single thread.

  • Are the threads prioritised to whichever piece of code is run first?

Tasks are assigned to a thread. A task will not change which thread it runs on. If a task needs to run in another thread, then it creates a new task and assigns that new task to the other thread.

  • What is the difference between main queue and thread?

The main queue is a dispatch queue which has 1 thread. This single thread is also known as the main thread.

  • Can threads run at the same time?

Threads are assigned to execute on processors by the operating system. If your device has multiple processors (they all do now-a-days), then multiple threads are executing at the same time.

  • If so how can I specify which parts of my code should run at a selected thread?

Break you code into tasks. Dispatch the tasks on a dispatch queue.

Implementation of swift's synchronized not working

First of all, never test these things in a playground, as its output will not correctly simulate reality. Test in an actual app project.

Second, get rid of all this:

    objc_sync_enter(lock)
defer { objc_sync_exit(lock) }
closure(name)

Do whatever it is you want done purely using GCD.

I'm not clear on what you do want done; the kerfuffle with objc_sync_enter and asyncAfter is just too mystifying for me. This stuff, however, is not at all mystifying, and is very well documented and explained (contrary to your claim).

From your desired output, it looks like you want to queue the "thread 1" operation and have it run from start to finish before the "thread 2" operation starts.

  • One way to guarantee that is to queue these operations onto a serial queue, because that means that a queued operation cannot start until the queue ahead of it is empty. That's not happening in your code because you queue is a concurrent queue, meaning that the operations run at the same time (i.e. their steps can be interleaved with one another).
  • Another way would be to call the "thread 1" operation with sync rather than async, because that means your code waits (blocks) until the operation completes and returns.

Here's an example of the first way, which I think is the better way:

let queue = DispatchQueue(label:"myqueue") // serial queue
func go() {
for i in 0..<4 {
let name = "Thread \(i)"
queue.async {
for i in 0..<4 {
let str = "\(name) :: \(i)"
print(str)
}
}
}
}

Output when we call go():

Thread 0 :: 0
Thread 0 :: 1
Thread 0 :: 2
Thread 0 :: 3
Thread 1 :: 0
Thread 1 :: 1
Thread 1 :: 2
Thread 1 :: 3
Thread 2 :: 0
Thread 2 :: 1
Thread 2 :: 2
Thread 2 :: 3
Thread 3 :: 0
Thread 3 :: 1
Thread 3 :: 2
Thread 3 :: 3

That looks a lot like what you said you wanted.

Log which queue/thread a method is running on

You can get the current thread with +[NSThread currentThread]. That could have a name property, but if you didn't set one don't rely on it.

Queues are trickier because there are different meanings of "queue". A queue could be an NSOperationQueue, and you can grab its name from +[NSOperationQueue currentQueue] (again, assuming you set it).

Then there are dispatch queues. You can get the current queue with dispatch_get_current_queue(), but be warned that this function will succeed even if called from code that isn't associated with a queue(!). In that case it returns the default background queue Queues are labeled, so you can call dispatch_queue_get_label() and if you created the queue with a label, you will get that.

So basically, yes you can get the queue or thread—with the proviso that all code has an associated dispatch queue even if it isn't code that was dispatched. You can also usually get meaningful names for these threads and queues, which is handy for debugging: but it's your responsibility to name them.

Difference in scheduling NSTimer in main thread and background thread?

NSTimer requires an active run loop, when initialized in Main Thread it automatically uses the main run loop. If you need to make a background timer you need attach it to the thread’s run loop and invoke run() to make it active.

  1. NSTimer needs one live NSRunLoop to execute it’s events. In main thread, the NSRunLoop is always live and will never stop until the app is terminated, but in other threads, you must invoke run() to active the NSRunLoop.

  2. NSTimer must invoke invalidate() to release the current timer, otherwise, the timer will retain a strong reference of the current instance of target, and it will remain in memory until invalidate() invoked or app terminated;

  3. NSTimer must created and invalidated in the same thread, and a lot of times, we may forget that.

Take a look at this example , it may be helpful >> http://www.acttos.org/2016/08/NSTimer-and-GCD-Timer-in-iOS/
and the documentation : https://developer.apple.com/documentation/foundation/nstimer



Related Topics



Leave a reply



Submit