2010-07-21 72 views
0

我是java开发人员,需要在iPhone中进行线程同步。我有一个线程,它调用另一个线程,需要等待该子线程结束。 在java我使用监视器,通过呼吁等待/通知iphone线程同步

我怎样才能在iphone中编程?

感谢

回答

0

就个人而言,我更喜欢pthreads。要阻止某个线程完成,您需要pthread_join或者,您可以设置一个pthread_cond_t并让调用线程等待,直到子线程通知它。

void* TestThread(void* data) { 
    printf("thread_routine: doing stuff...\n"); 
    sleep(2); 
    printf("thread_routine: done doing stuff...\n"); 
    return NULL;  
} 

void CreateThread() { 
    pthread_t myThread; 
    printf("creating thread...\n"); 
    int err = pthread_create(&myThread, NULL, TestThread, NULL); 
    if (0 != err) { 
     //error handling 
     return; 
    } 
    //this will cause the calling thread to block until myThread completes. 
    //saves you the trouble of setting up a pthread_cond 
    err = pthread_join(myThread, NULL); 
    if (0 != err) { 
     //error handling 
     return; 
    } 
    printf("thread_completed, exiting.\n"); 
}