如何在 Objective-C 中创建原子函数
有没有办法原子地执行整个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
线程编程中有大量信息指南。它特别提到避免同步(这很有趣,因为有时你不能),但他们针对这个问题提供了一些建议。
如果您开始在多核上运行软件,您的设计将会遇到严重的问题。停止所有内核运行来运行您的代码是一个非常昂贵的操作。互斥体、信号量、运行循环事件和原子操作是可行的方法。
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.
没有。不能那样做。
或者,好吧,如果您足够深入地研究 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?