# Arc | Learn Rust part 62

## Метаданные

- **Канал:** Smart Contract Programmer
- **YouTube:** https://www.youtube.com/watch?v=qXCsNHpV4Q8

## Содержание

### [0:00](https://www.youtube.com/watch?v=qXCsNHpV4Q8) Segment 1 (00:00 - 04:00)

for a single threaded program. If you want to share data and also have the ability to mutate it, then one common approach is to use the RC and ref cell. However, these two constructs are not safe to be used for multi-threaded programs. For multi-threaded programs, one common approach is to use the arc and mutex. I've already explained how to use mutex in the previous video. So, in this video, I'll explain how to use arc. First of all, why is it named arc? RC stands for reference count and ARC stands for atomic reference count. Basically, it's the multi-threaded version of RC. Okay, so next, let me show you how to use this arc. In the previous video, we created a mutex and then spawn two threads using scope threads. Inside each thread, we acquired a lock from a mutex and then modify the data behind the mutex. In this video, we're going to be using the arc to accomplish the same thing. spawn two threads modify the data behind the mutx. So first of all I'll copy this code and then paste it over to a new file. We begin by creating a mutx. Now in the single threaded case we had a ref cell. This represents a data where we can pass as immutable reference but we also have the ability to mutate the data. And then to be able to share this data we wrap this in a RC. For a multi-threaded program we do something similar. We replace RC with arc and then we replace ref cell with mutx. Okay, so in our code example, we have a mutx. Next, let's wrap this in arc. We'll put this mutex in arc new. Let's name this as counter. Next, let's spawn a thread. And then inside the thread, we'll update the data behind the mutex. Thread spawn. Inside the closure, To start off with, I'll simply copy this code and then paste it here. We don't need this panic macro. I'll also remove the type definition. Okay, so this is our starting point, but the code does not compile at the moment. First, let's try to move this arc inside the closure. So here, instead of referencing the mutex, we'll reference the counter. And then since we're using the counter inside this closure, we also need to move the ownership of the ark. And now the code compiles. But how do we spawn two threads? and inside each thread modify the data behind the mutex. Currently, we're only spawning a single thread. Okay, so let's try creating another thread. Spawning another thread. And of course, the code does not compile since we moved the ownership of the arc into the first thread. Okay, so how do we fix this? Remember that RC is a construct that is able to share data for singlethreaded programs. For multi-threaded programs, we will use arc. Arc will allow us to share data across threads. To do this, we first need to clone this arc. Let's say that C1 is equal to arc clone and then pass in the counter. To create another reference, we say C2 and then call clone again passing in the reference to this arc over here. And then inside each thread, instead of passing the original counter, we'll pass in the clone C1 and C2. And now our code compiles again. And finally, let's print the value after spawning two threads, updating the value behind the mutex. Since we're spawning, we also need to wait for this thread to finish before the main function can finish execution. We'll do this by assigning them to a handle. Let's say H1 and then H2 and then wait on the results. H1. join. wrap. And we'll do the same for H2. After the threads are done executing the code, we'll print out the value behind the mutex. And that is it. We translated the code where it used scope threads to spawn two threads and update the data behind mutex into code that accomplishes the same thing except instead of using scope thread it is using arc. Let's execute this code. Execute the code and we get the values arc a mutex with data equal to two. So in this video I showed you example of how to use arc and mutex to be able to share and modify data across threads. To summarize what we did, we first created a mutex and then put it behind an arc. To use this arc in a thread that we spawn, we first clone the ark and then pass in the clone value into the thread.

---
*Источник: https://ekstraktznaniy.ru/video/39146*