0

I am currently learning IOS Threading programming... I encountered an issue:

Here comes my code, please kindly have a look:

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        NSThread *t1 = [[NSThread alloc]initWithTarget:[MyThread class] selector:@selector(myMethod:) object:Nil];
        [t1 start];
    }
    return 0;
}

#import "MyThread.h"

@implementation MyThread

+ (void)myMethod:(id)param
{
    @autoreleasepool {
        NSLog(@"called...");
    }
}

@end

However, when I ran my program, though there was no error, no message was printed on the console. It seems like myMethod was not executed. I wonder if anyone could give me some suggestions. It has already driven me crazy.

Many thanks in advance.

David Dai
  • 1,095
  • 2
  • 8
  • 11

2 Answers2

1

The main thread of your application is exiting before your other thread has a chance to process anything.

It will work if you add in a simple sleep(1000) statement anywhere before the return 0 statement in your main method.

Harry
  • 3,076
  • 4
  • 28
  • 44
  • Yes, Harry, you are absolutely right. I just notice that and I modified my code and I saw my new created thread running. I just wanna answer my question however found you answered it few minutes ago. Once again, thank you very much. – David Dai Jul 25 '13 at 13:50
  • Also you should add the sleep method within isExecuting: do { sleep(100); } while (t1.isExecuting); – Shams Ahmed Jul 25 '13 at 13:51
  • @Harry Hello Both, I wonder is it different from Java Thread Programming? As I also write a similar program in Java, however, the main thread won't exit until child thread has done something even I forced the child thread to sleep for 50 secs – David Dai Jul 25 '13 at 14:09
  • @ShamsAhmed Hello Both, I wonder is it different from Java Thread Programming? As I also write a similar program in Java, however, the main thread won't exit until child thread has done something even I forced the child thread to sleep for 50 secs – David Dai Jul 25 '13 at 14:10
  • I'm not too familiar with Java multi-threading, but yes, definitely sounds like they're different! – Harry Jul 25 '13 at 14:20
1

Your application is terminating before the thread has executed the NSLog.

NSThread creates a detached thread, see Apple's Thread Programming Guide, from which comes:

Important: At application exit time, detached threads can be terminated immediately but joinable threads cannot. Each joinable thread must be joined before the process is allowed to exit. Joinable threads may therefore be preferable in cases where the thread is doing critical work that should not be interrupted, such as saving data to disk.

To create a joinable thread, and hence be able to block your main thread until all joinable threads have finished, you use pthread - covered in the above Guide.

The Java thread model is similar, but uses slightly different terminology. By default a Java thread is joinable and the Java application will continue to execute until all such threads have terminated. A Java thread can be converted to a daemon thread, which is automatically terminated on application exit as with NSThread threads.

CRD
  • 52,522
  • 5
  • 70
  • 86
  • Thank you so much for your answer. I really appreciate it. I have a little question: you said that we could create a pThread which could be able to block my main thread. I wonder what does block mean, does it mean main thread could not do anything until pThread finish? BTW, from Java Side, I always use joinable thread like what you mentioned. May I know how to convert it to daemon thread? And is it good to use daemon thread in java and pThread in iOS? Once again, thank you very much. – David Dai Jul 26 '13 at 00:13
  • @DavidDai - sorry, I could have written that a bit clearer: with a pthread you can *choose* to block your main thread while waiting for a thread to complete, you do not have to! The call is `pthread_join`, with an obvious similarity to the Java model you know. The Java method you're looking for is `setDaemon`. – CRD Jul 26 '13 at 00:53
  • Hi, thank you so much for your further detail. It is very useful. I appreciate it. Hope you have a good night. – David Dai Jul 26 '13 at 08:50