# Thread lifetime for TLS

**URL:** <https://internals.rust-lang.org/t/thread-lifetime-for-tls/13550>\
**Category:** language design\
**Created:** [December 10, 2020, 9:53pm UTC](https://internals.rust-lang.org/t/thread-lifetime-for-tls/13550 "2020-12-10T21:53:46Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![matklad](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/matklad/32/12266_2.png) [@matklad](https://internals.rust-lang.org/u/matklad)\
**Post date:** [December 11, 2020, 4:13pm UTC](https://internals.rust-lang.org/t/thread-lifetime-for-tls/13550/7 "2020-12-11T16:13:46Z")

</div>

The hard bit about thread-locals are destructors. What `with` achieves is a lazy re-initialization of thread-locals.

In other words, what's the semantics of the following program?

```rust
#[thread_local]
static A: S = S(0);

#[thread_local]
static B: S = S(1);

struct S(u32);
impl Drop for S {
  fn drop(&mut self) {
    println!("{:?}", (A.0, B.0));
  }
}

fn main() {
  std::thread::spawn(|| {
    (A.0, B.0);
  }).join().unwrap();
}

```

---

_[View the full topic](https://internals.rust-lang.org/t/thread-lifetime-for-tls/13550)._
