# How to use self while spawning a thread from method

**URL:** <https://users.rust-lang.org/t/how-to-use-self-while-spawning-a-thread-from-method/8282>\
**Category:** help\
**Created:** [December 6, 2016, 10:18am UTC](https://users.rust-lang.org/t/how-to-use-self-while-spawning-a-thread-from-method/8282 "2016-12-06T10:18:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![aabcor](https://avatars.discourse-cdn.com/v4/letter/a/41988e/32.png) [@aabcor](https://users.rust-lang.org/u/aabcor)\
**Post date:** [December 6, 2016, 10:18am UTC](https://users.rust-lang.org/t/how-to-use-self-while-spawning-a-thread-from-method/8282/1 "2016-12-06T10:18:40Z")

</div>

Hello,

I am new to Rust and I am trying to make the following code work. Basically, I need to spawn a thread from a type instance's method and I need to use the self reference inside the thread closure. I am aware that the compiler is complaining because the lifetime of self is shorter than static lifetime, but I'm not sure how to deal with this scenario appropriately. Eventually, I need my thread to be able to access the fields and methods of self. Should I make self static? How? What if I had more than one instance of my type?

Any help would be greatly appreciated. Thanks.

Code:

```rust
use std::thread;

struct ThreadTest<'a> {

	string : &'a str,
	
}

impl<'a> ThreadTest<'a> {

	fn new(string: &'a str) -> ThreadTest<'a> {

		ThreadTest {string: string}
	}

	fn start (&'a mut self) {

		thread::spawn(move || {

			self.test();
		});
		
	}

	fn test(&self) {

		println!("{:?}", self.string);
	}

}

fn main() {

	let mut thread_test = ThreadTest::new("this is a string");
	thread_test.start();

	loop {};
}

```

---

<div class="post-metadata">

**Author:** ![krdln](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/krdln/32/16764_2.png) [@krdln](https://users.rust-lang.org/u/krdln)\
**Post date:** [December 6, 2016, 10:30am UTC](https://users.rust-lang.org/t/how-to-use-self-while-spawning-a-thread-from-method/8282/2 "2016-12-06T10:30:49Z")

</div>

There are a few similar topics on this forum, which should be helpful:  
[https://users.rust-lang.org/search?q=spawn%20thread%20self](https://users.rust-lang.org/search?q=spawn%20thread%20self)

Short answer: if the thread is short-lived (finishes before the method's end), use a [scoped thread](https://docs.rs/crossbeam/0.3.2/crossbeam/struct.Scope.html#method.spawn) (eg. from the crossbeam crate). If the thread is long-lived, put the fields of your struct (or even the whole struct) in `std::sync::Arc`. Also, you will probably have to change `&str` to `String`.

PS. Put your code in a fence when posting, like that:

````
```rust
some_code
```

````

---

<div class="post-metadata">

**Author:** ![aabcor](https://avatars.discourse-cdn.com/v4/letter/a/41988e/32.png) [@aabcor](https://users.rust-lang.org/u/aabcor)\
**Post date:** [December 6, 2016, 11:07am UTC](https://users.rust-lang.org/t/how-to-use-self-while-spawning-a-thread-from-method/8282/3 "2016-12-06T11:07:33Z")

</div>

Thank you for your answer. I'm not sure though how to put the whole struct in an Arc. I tried the following but it didn't compile:

```rust

// in the implementation of the structure:

	fn start (&'a mut self) {

		let shared_self = Arc::new(self);

		let self_copy = shared_self.clone();

		thread::spawn(move || {

			let local_self = &self_copy;

			local_self.test();
		});
		
	}

```

Error is: the lifetime cannot outlive the lifetime 'a as defined on the block

Any working example on how to Arc the whole structure instance please?

Thanks

---

<div class="post-metadata">

**Author:** ![krdln](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/krdln/32/16764_2.png) [@krdln](https://users.rust-lang.org/u/krdln)\
**Post date:** [December 6, 2016, 12:11pm UTC](https://users.rust-lang.org/t/how-to-use-self-while-spawning-a-thread-from-method/8282/4 "2016-12-06T12:11:44Z")

</div>

To put the whole struct in `Arc` you'll just need to actually two structs – the inner one and the outer one (although wrapping separate fields would be a better idea, especially if you don't want the thread to acces the whole struct). You can implement `start` only on the outer one.

```rust
struct MyInner { s: String }
struct My { inner: Arc<MyInner> }

impl My {
	fn start(&self) {
		let local_self = self.inner.clone();
		thread::spawn(move || {
			local_self.test();
		});
	}
}

```

[Playground link](https://play.rust-lang.org/?gist=f12c8845538388426fd149bec2c757c3&version=stable&backtrace=0)

Note that I've changed `&str` to `String`, which was a direct cause of the error (you can't share references in non-scoped threads).

Note that if you want the thread to make changes to original fields, you'll need to wrap this field in a `Mutex` or some `atomic` type.

---

<div class="post-metadata">

**Author:** ![aabcor](https://avatars.discourse-cdn.com/v4/letter/a/41988e/32.png) [@aabcor](https://users.rust-lang.org/u/aabcor)\
**Post date:** [December 7, 2016, 8:19am UTC](https://users.rust-lang.org/t/how-to-use-self-while-spawning-a-thread-from-method/8282/5 "2016-12-07T08:19:56Z")

</div>

Thank you so much. It actually works and I am able to read the string field from my thread. However, I am struggling to implement the ability to change this string. As you can see, I wrapped the field in a Mutex but the compiler is telling me that I "cannot borrow immutable borrowed content as mutable" -- although I did make sure that both self and local\_self are mutable in my start() function:

```rust

use std::thread;
use std::sync::Arc;
use std::sync::Mutex;

struct MyInner { s: Mutex<String> }
struct My { inner: Arc<MyInner> }

impl My {

	fn new(s: String) -> My {

		My {inner: Arc::new(MyInner {s: Mutex::new(s)})}
	}

	fn start(&mut self) {

		let mut local_self = self.inner.clone();

		thread::spawn(move || {
			local_self.test();
		});
	}
}

impl MyInner {
	
	fn test(&mut self) {

		println!("{:?}", self.s);
		self.s = Mutex::new(String::from("Hello!"));
	}
}

fn main() {

	let mut thread_test = My::new(String::from("this is a string"));
	thread_test.start();

	loop {};
} 

```

Thank you for your help!

---

<div class="post-metadata">

**Author:** ![aabcor](https://avatars.discourse-cdn.com/v4/letter/a/41988e/32.png) [@aabcor](https://users.rust-lang.org/u/aabcor)\
**Post date:** [December 7, 2016, 1:48pm UTC](https://users.rust-lang.org/t/how-to-use-self-while-spawning-a-thread-from-method/8282/6 "2016-12-07T13:48:03Z")

</div>

OK I got it right now.. In fact, I had to wrap the inner struct itself in a Mutex.. Thanks again krdln. Here's the final code to access the inner struct instance from the thread in read/write mode:

```rust

use std::thread;
use std::sync::Arc;
use std::sync::Mutex;

struct MyInner { s: String }
struct My { inner: Arc<Mutex<MyInner>> }

impl My {

	fn new(s: String) -> My {

		My {inner: Arc::new(Mutex::new(MyInner {s: s}))}
	}

	fn start(&mut self) {

		let local_self = self.inner.clone();

		thread::spawn(move || {

			local_self.lock().unwrap().test();
		});
	}
}

impl MyInner {
	
	fn test(&mut self) {

		println!("{:?}", self.s);
		self.s = String::from("and this is a modified string");
		println!("{:?}", self.s);
	}
}

fn main() {

	let mut thread_test = My::new(String::from("this is a string"));
	thread_test.start();

	loop {};
} 

```

---

<div class="post-metadata">

**Author:** ![krdln](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/krdln/32/16764_2.png) [@krdln](https://users.rust-lang.org/u/krdln)\
**Post date:** [December 7, 2016, 6:13pm UTC](https://users.rust-lang.org/t/how-to-use-self-while-spawning-a-thread-from-method/8282/7 "2016-12-07T18:13:47Z")

</div>

I'm happy you've made it work! You can also make the previous (with the `String` directly inside of `Mutex`) version work. You just need to change the signature of `test` to take `&self`, not `&mut self` and just use the existing mutex instead of creating a new one:

```
*self.s.lock().unwrap() = String::from("hello");

```

The reason you need `&self`, not `&mut self`, despite you're actually mutating the struct, is because `&` is actually a _shared_ reference, and `&mut` is an _unique_ reference. For the "regular" types, like `i32` or `String` shared also means immutable and unique means mutable.

In your case (still talking about the first solution), your `test` function definitely can't have a _unique_ access to `MyInner`, since it's shared between threads. It's the `Mutex`, that can temporarily give a _unique_(`mut`) access to its contents.

In your second solution, you've worked around this problem by putting the whole struct inside a `Mutex`, so `test` function can have _unique_ access, as the struct is never shared directly (the `Mutex` is shared, the struct is "hidden" inside it). It's definitely working, as you see, but it's less elegant solution (imagine the struct had some other read-only fields, they would unnecessarily need `.lock()` to be read).

You should also try [channels](https://static.rust-lang.org/doc/master/std/sync/mpsc/index.html)! They're more efficient and less error-prone than mutexed fields. Maybe they fit your needs.

Also, I recommend [post series about interior mutability](https://ricardomartins.cc/2016/06/08/interior-mutability), especially the second part, which explains sharing mutable memory between threads.

---

<div class="post-metadata">

**Author:** ![aabcor](https://avatars.discourse-cdn.com/v4/letter/a/41988e/32.png) [@aabcor](https://users.rust-lang.org/u/aabcor)\
**Post date:** [December 14, 2016, 10:26am UTC](https://users.rust-lang.org/t/how-to-use-self-while-spawning-a-thread-from-method/8282/8 "2016-12-14T10:26:30Z")

</div>

Thanks a lot for the info. I'll explore it...

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/steffahn/32/47569_2.png) [@steffahn](https://users.rust-lang.org/u/steffahn)\
**Post date:** [July 3, 2022, 2:45pm UTC](https://users.rust-lang.org/t/how-to-use-self-while-spawning-a-thread-from-method/8282/9 "2022-07-03T14:45:48Z")

</div>

Closed on July 3, 2022, 2:45pm UTC


