如何在 Objective-C 中创建原子函数

发布于 2024-09-07 15:34:07 字数 117 浏览 2 评论 0原文

有没有办法原子地执行整个 Objective-C 函数?

据我所知,使用synchronized只能保护特定的代码不被同时在多个线程上执行。但我想要的是,只要我执行该函数,就阻止所有其他线程执行任何操作。

Is there a way to execute a whole objective-c function atomic?

As far as I know, using synchronized only protects a specific peace of code from being executed on multiple threads at the same time. But what I want is stop ALL other threads from doing ANYTHING, as long as I execute the function.

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

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

发布评论

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

评论(2

满栀 2024-09-14 15:34:07

线程编程中有大量信息指南。它特别提到避免同步(这很有趣,因为有时你不能),但他们针对这个问题提供了一些建议。

如果您开始在多核上运行软件,您的设计将会遇到严重的问题。停止所有内核运行来运行您的代码是一个非常昂贵的操作。互斥体、信号量、运行循环事件和原子操作是可行的方法。

There is a wealth of info in the Threading Programming Guide. It specifically mentions to avoid synchronization (which is funny, cause you cant sometimes) but they offer some suggestions around the problem.

You will have serious problems with your design if you start running your software on multicore. It is a VERY expensive operation to stop all cores from running to run your bit of code. Mutexes, semaphores, run loop events, and atomic operations are the way to go.

若水般的淡然安静女子 2024-09-14 15:34:07

没有。不能那样做。

或者,好吧,如果您足够深入地研究 Mach API(无论如何在 Mac OS X 上),您可能可以。

但你不应该这样做。

你认为你为什么要这么做?

Nope. Can't do that.

Or, well, you probably could if you dipped deep enough in the Mach APIs (on Mac OS X anyway).

But you shouldn't do that.

Why do you think you want to do that?

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