# Thread safety of weak properties

**URL:** <https://forums.swift.org/t/thread-safety-of-weak-properties/422>\
**Category:** Compiler\
**Created:** [December 11, 2015, 2:50am UTC](https://forums.swift.org/t/thread-safety-of-weak-properties/422 "2015-12-11T02:50:53Z")\
**Posts on this page:** 1\
**Showing post:** 17

<div class="post-metadata">

**Author:** ![gparker42](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gparker42/32/373_2.png) [@gparker42](https://forums.swift.org/u/gparker42)\
**Post date:** [December 16, 2015, 2:01am UTC](https://forums.swift.org/t/thread-safety-of-weak-properties/422/17 "2015-12-16T02:01:59Z")

</div>

It uses Mach's thread\_switch() and passes a Mach thread port. It also uses a private option flag; I don't know if any of the public ones are good enough to solve the problem.

> **···**
>
> > On Dec 15, 2015, at 5:32 PM, Kevin Ballard \<kevin@sb.org\> wrote:
> > 
> > > On Tue, Dec 15, 2015, at 01:38 PM, Greg Parker via swift-dev wrote:
> > > 
> > > Another solution is to use a handoff lock algorithm. This is what libobjc does now. The lock owner stores its thread ID in the lock. Each lock waiter yields to the owner thread specifically, donating its priority and resolving the inversion. This scheme has theoretical holes when multiple locks are involved, but in practice we haven't seen any problems.
> > > 
> > > As far as I know the spinlock that libobjc now uses is not API.
> > 
> > It's not. I checked earlier today against the copy of the objc runtime I have (from opensource.apple.com; my copy is slightly out of date, at version 647, but it's new enough). The runtime defines a type spinlock\_t that's backed by something called os\_lock\_handoff\_s. It also imports a header \<os/lock\_private.h\>, which is presumably where this type is defined. I already updated my radar to suggest that perhaps this type would be a good candidate to expose.
> > 
> > Incidentally, if I wanted to implement my own spinlock like this, how can you yield to a specific thread? I'm only aware of pthread\_yield\_np(), which doesn't take a target thread.
> 
> --  
> Greg Parker gparker@apple.com Runtime Wrangler

---

_[View the full topic](https://forums.swift.org/t/thread-safety-of-weak-properties/422)._
