避免从同时使用的访问器获取已释放的对象

发布于 2024-12-29 22:44:44 字数 564 浏览 3 评论 0原文

在我的多线程应用程序中,有一个属性可以被多个线程同时访问。该属性定义为@property (retain) NSObject *status

如何以原子方式获取并保留该属性,以便我可以在本地范围内安全地使用它?存储和检索这样的值的最佳实践是什么?

NSObject *status = [self status];
[... do some processing on status ...]

// But: I cannot rely on the object assigned to my status variable 
// still being valid, since another thread might have used my 
// [self setStatus] accessor, implicitly calling a release on the old object, 
// releasing it under my feet.
// Not even NSObject *status = [[self status] retain]; would fix that.

In my multithreaded application, there is a property that can be accessed concurrently by multiple threads. The property is defined as @property (retain) NSObject *status.

How do I atomically get and retain the property, so I can safely use it in my local scope? What is the best practice to store and retrieve such a value?

NSObject *status = [self status];
[... do some processing on status ...]

// But: I cannot rely on the object assigned to my status variable 
// still being valid, since another thread might have used my 
// [self setStatus] accessor, implicitly calling a release on the old object, 
// releasing it under my feet.
// Not even NSObject *status = [[self status] retain]; would fix that.

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

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

发布评论

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

评论(1

何止钟意 2025-01-05 22:44:44

在我的多线程应用程序中,有一个属性可以被多个线程同时访问。该属性定义为 @property (retain) NSObject *status。

原子是默认值 - 不需要关键字或说明符。

如何以原子方式获取并保留该属性,以便我可以在本地范围内安全地使用它?

由于它是原子的、保留的、读写的和@synthesized 的,因此您可以始终使用访问器。如果您直接在初始化程序中访问 ivar 并仅在 dealloc 中访问,并在其他地方使用访问器,那么就您所询问的问题而言,它将是安全的。返回值是retain+autoreleased。由于它是自动释放的,因此该对象将继续存在——至少直到本地线程上的顶部自动释放池被弹出为止。

上次我走过时,它看起来像这样(以简单的形式):

- (NSObject *)object
{
    enter_object_level_spin_lock();
    id ret = [[object retain] autorelease];
    exit_object_level_spin_lock();
    return ret;
}

虽然我不记得 autorelease 是否在锁中(理想情况下它应该在外面,以缩短锁定时间) )。

存储和检索此类值的最佳实践是什么?

原子属性对于并发性或线程安全性作用很小——不要认为它们可以替代适当的线程安全性。原子属性访问器提供的内容涵盖线程安全也是不寻常的。通常,对于适当的并发程序,您需要的不仅仅是一个原子。我很少使用原子属性,并制作一些疯狂的并发程序。如果您需要高性能/事务并发性,您将需要知道如何有效地使用锁。当然,也有很多方法可以避免并发设计。我的自动解决方案涉及自行处理锁定。

In my multithreaded application, there is a property that can be accessed concurrently by multiple threads. The property is defined as @property (retain) NSObject *status.

atomic is the default - there is no keyword or specifier required.

How do I atomically get and retain the property, so I can safely use it in my local scope?

Since it's atomic, retained, readwrite and @synthesized, you can just always use the accessors. Provided you access the ivar directly in your initializers and dealloc only -- and use the accessors everywhere else, it will be safe in the regard which you're asking about. The return value is retain+autoreleased. Since it's autoreleased, the object will live on -- at least until the top autorelease pool on the local thread is popped.

The last time I stepped through, it looked something like this (in simple form):

- (NSObject *)object
{
    enter_object_level_spin_lock();
    id ret = [[object retain] autorelease];
    exit_object_level_spin_lock();
    return ret;
}

although i don't remember if the autorelease was in the lock or not (it would ideally be outside for shorter lock times).

What is the best practice to store and retrieve such a value?

Atomic properties do very little for concurrency or thread safety -- don't consider them a substitute for proper thread safety. It's also unusual for thread safety to be covered by what atomic property accessors provide. Typically, you will need more than an atomic for a proper concurrent program. I very very rarely use atomic properties, and make some crazy concurrent programs. If you need high performance/transaction concurrency, you will need to know how to use locks effectively. Of course, there are many ways to avoid concurrent designs as well. My automatic solution involves handling locking myself.

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