views:

1294

answers:

4

I was told that the iPhone does not support multitasking and multithreading. This did not make sense to me, so I tested on the simulator: pthreads works, fork() doesn't. This result does make sense to me, but now I'm not sure: will the pthread library also work on the real device?

Thanks.

+1  A: 

Most likely.

Multitasking is disabled by default to prevent apps from spawning a bunch of processes and either slowing down the iPhone or doing malicious things.

The iPhones CPU really isn't that fast, but by only running 1 program at a time, it seems speedy. Multitasking would introduce a lot of overhead and other problems which would slow down the iPhone.

I'm not actually sure about multithreading, but since threads are contained to your own process, it seems likely that they would work.

And as you said, pthreads work and fork() doesn't, so its logical it would work on the real one as well.

samoz
+6  A: 

Multithreading will work. It's multitasking that won't. The iphone won't let more than one third party application run at once. That reasoning makes fork live outside of the application's sandbox.

You can create threads to poll sockets, read files, handle an AI player all you want, or until the performance gains start to go away.

Ball
A: 

Multithreading is very much possible - the iPhone actually uses the same Cocoa threading APIs that are available on the Mac. I write a collaborative drawing app that uses 6 threads to handle drawing, network communication, etc. I think creating too many threads would be a bad idea, since the iPhone only has one processor. They work very well in my experience, though!

Ben Gotow
+2  A: 

Yes, the pthread library will work on the iPhone. Alternately you can use Cocoa-native threads with NSThread. Multitasking will not work, as Apple is explicitly restricting that.

pix0r