Arc

There's another layer of complexity when it comes to sharing data between multiple code blocks: threads and multithreading. Rc, Cell, and RefCell are all impossible to share between threads, but the ideas they represent would be useful for enabling communication between threads.

There's a direct equivalent of Rc for use with threads: Arc. An Arc is an atomic reference-counted smart pointer, which is valid for sharing between threads thanks to that atomic, which basically means that even if two threads try to use it at the same time, it's not going to get messed up or confused.

Arc has a different name and works differently inside, but on the surface it's just like Rc. The things we've learned about how to use an Rc apply to an Arc as well.

It's hard to demonstrate the special features of Arc without using Mutex or RwLock as well, so see the next section for some example code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.28.179