I am java developer and need to make thread synch in iPhone. I have a thread开发者_开发知识库, it calls other one and need to wait for that child thread to end. In java I use monitor, by calling wait/notify
How can I program this in iphone?
thanks
NSConditionLock does all job
Read about NSOperation
dependencies and also NSNotification
notifications.
Personally, I prefer pthreads. To block for a thread to complete, you want pthread_join
Alternately, you could set up a pthread_cond_t
and have the calling thread wait on that until the child thread notifies it.
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");
}
精彩评论