Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When Would Anyone Want To Use NSThreads over the GCD?

Are there any cases when anyone would want to use raw NSThreads instead of GCD for concurrency? I love the GCD, but I want to know if I will need to use NSThreads for Cocoa/Cocoa-Touch eventually.

like image 831
Andy Ibanez Avatar asked Aug 10 '12 21:08

Andy Ibanez


People also ask

What is the difference in creating the thread using GCD and using Nsthread?

From what I read online, GCD seems to do the exact same thing as basic threads (POSIX, NSThreads etc.) while adding much more technical jargon ("blocks"). It seems to just overcomplicate the basic thread creation system (create thread, run function).

What is an advantage of using NSOperation vs GCD?

Once you dispatch a task using Grand Central Dispatch, you no longer have control or insight into the execution of that task. The NSOperation API is more flexible in that respect, giving the developer control over the operation's life cycle.

Is GCD thread safe?

In apple document, GCD is Thread-Safe that means multiple thread can access.

What is the thread limit of GCD?

The 64 thread width limit of GCD is undocumented because a well-designed concurrency architecture shouldn't depend on the queue width. You should always design your concurrency architecture such that a 2 thread wide queue would eventually finish the job to the same result (if slower) as a 1000 thread wide queue.


1 Answers

i use pthreads for control, good performance, and portability. sometimes, you might opt to use NSThread for the extra NSObject interfacing it offers.

there are a few lower level interfaces where you need to coordinate threads with the APIs you use (e.g. realtime I/O or rendering). sometimes you have flexibility regarding the thread you use, sometimes it is convenient to use NSThread in this situation so you can easily use CF or NS run loops with these interfaces. So the run loop parameter you set up on your thread is likely of more interest to the API than the thread itself. in these cases, GCD may not necessarily be an alternative.

but… most devs won't need to drop to these levels often.

like image 190
justin Avatar answered Nov 09 '22 14:11

justin