NSInvocationOperation Valid Equivalent

I am porting an Android app that I made for iOS. Android has a function Yield()

to move a thread out of the reverse (?) Thread queue. This is useful to keep this thread from hanging too much CPU and making everything else sluggish. It works fine in my android app.

I am using objects NSInvocationOperation

to implement my streams. How to add functionality similar to Android (POSIX) Yield()



source to share

4 answers

I am using NSInvocationOperation objects to implement my threads.

It doesn't make a lot of sense. NSOperations run on a thread, but they are not themselves threads, and they will not allow you to implement anything equivalent to a thread. If you really want to use a stream, use NSThread

or pthread


How to add functionality similar to Android (POSIX) Yield ()?

If you really want POSIX try it sched_yield()

. At a higher level there pthread_yield_np()

(np means not portable - not in POSIX pthread_yield()

), but it does nothing other than call sched_yield()


I wouldn't bother until I discovered that you really need it and that it helps. Not quite right to do this sort of thing in iOS or Mac apps.



Have you dropped by the Grand Central Dispatch? This is one of the best ways to write multi-threaded code on iOS. (Of course, this is not a perfect solution for all threading problems, so it depends on your application.) As an example, GCD offers you lower priority queues for operations that are performance critical.

The usual way to write a modern iOS application is to keep only the UI code on the main thread (≈ in the main GCD queue) and offload other operations to one of the global GCD queues. Blocks from these queues do not dig out the main thread, they are unloaded into some kind of background thread (managed by the system, not you). It's very simple from a programmer's point of view and it seems to work pretty well.



I suggest you use NSOperationQueue with subclass NSOperations. This will help.



You can use GCD for this:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    // Do the heavy work
    // ...

    dispatch_async(dispatch_get_main_queue(), ^{

        // Reflect the result in the UI
        // ...





All Articles