iPhone 线程同步

发布于 2024-09-11 00:58:10 字数 131 浏览 3 评论 0原文

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

如何在iphone中对此进行编程?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

会发光的星星闪亮亮i 2024-09-18 00:58:10

NSConditionLock 完成所有工作

NSConditionLock does all job

那些过往 2024-09-18 00:58:10

阅读NSOperation 依赖项 以及 NSNotification 通知。

Read about NSOperation dependencies and also NSNotification notifications.

就我个人而言,我更喜欢 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");
}

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");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文