首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iphone线程同步

iphone线程同步
EN

Stack Overflow用户
提问于 2010-07-22 04:33:32
回答 3查看 783关注 0票数 0

我有一个线程,它调用另一个线程,并需要等待该子线程结束。

如何在iphone中编写此程序?

谢谢

EN

回答 3

Stack Overflow用户

发布于 2010-07-22 04:45:05

NSConditionLock可以完成所有工作

票数 1
EN

Stack Overflow用户

发布于 2010-07-22 04:40:29

阅读有关NSOperation dependenciesNSNotification通知的信息。

票数 0
EN

Stack Overflow用户

发布于 2010-07-22 04:59:49

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

代码语言:javascript
复制
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");
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3303578

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档