我是java開發人員,需要在iPhone中進行線程同步。我有一個線程,它調用另一個線程,需要等待該子線程結束。 在java我使用監視器,通過呼籲等待/通知iphone線程同步
我怎樣才能在iphone中編程?
感謝
我是java開發人員,需要在iPhone中進行線程同步。我有一個線程,它調用另一個線程,需要等待該子線程結束。 在java我使用監視器,通過呼籲等待/通知iphone線程同步
我怎樣才能在iphone中編程?
感謝
NSConditionLock做所有的工作
就個人而言,我更喜歡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");
}