I’m trying to port a C console program over to iPhone. I have imported all the code into my project, and I’m calling it’s main() in an NSOperation when the user clicks a button on the UI.
Anyway, this program is complicated and creates many of its own threads. The program calls “exit()” many times as well- this would crash the entire iPhone app, but I really want the user to be able to restart the C program if it does it.
I’ve been working on cleaning up these exit conditions, and I’ve used pthread_exit() in some of the child threads the C program creates. However, when I call pthread_exit() in the thread that is created from the NSOperation object, the app’s main thread get’s a SIGABRT signal and the entire app crashes.
Am I correctly assessing the situation? I am new to iOS programming so this signal may be coming from elsewhere… And if I am correct, what is the best way to get around this?
(Oh, and about using “return 0” to exit this NSOperation thread: the condition I want to exit from is nested deep in C program functions, I want to avoid changing as much structure of the program as possible, since it already very complicated)
Thanks in advance for any help!
NSOperationQueueexecutes it’s operations on GCD queues. While those execute their jobs on pthreads those threads are private to GCD and you are not allowed to modify or exit them using pthread APIs. There is documentation on Compatibility with POSIX Threads, which explicitly states which pthread functions are allowed and which not.To solve your problem you probably should not let your pthread code run on a
NSOperationQueuebut on a new POSIX thread you create just for that.