我有一个线程,它调用另一个线程,并需要等待该子线程结束。
如何在iphone中编写此程序?
谢谢
发布于 2010-07-22 04:45:05
NSConditionLock可以完成所有工作
发布于 2010-07-22 04:40:29
阅读有关NSOperation dependencies和NSNotification通知的信息。
发布于 2010-07-22 04:59:49
就我个人而言,我更喜欢pthread。要阻止线程完成,您可以交替使用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");
}https://stackoverflow.com/questions/3303578
复制相似问题