5

Cloning, how does it work?

 2 years ago
source link: https://dev.to/milo123459/cloning-how-does-it-work-pgo
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Milo

Posted on Oct 21

Cloning, how does it work?

Hey!
Recently, I have been faced with an error. I have a DasHap of a &str and a bool, and I have 2 functions, each in a loop, each in their own thread. I need to share this dashmap between both threads, so I wrapped it in Arc. I get a borrowing error, of course, and was suggested to clone outside of the thread because the closure is async move. But, then, if I update the not-cloned value, that won't reflect on the cloned-value, and to update the cloned-value, I have to reference it where the not-cloned value changes, which is in a thread, that would cause borrowing errors.
To sum up, having a value being cloned outside of a loop, requiring the original value to be updated, subsequently also requires the cloned-value to be update, but can't be done as if I update the cloned-value in thread 1, I can't use it in thread 2 because of borrowing errors.

Any help appreciated!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK