I'm sharing a variable been threads in a Rust program. This variable is only ever read from in these threads, and never written to elsewhere following being passed to the threads (it is only written to before the threads are spawned). The variable should live as long as the program is running; if it no longer lives the entire program's functionality will fall apart. As for the program itself, it runs indefinitely. As for the threads, their spawning is facilitated by Tokio's tokio::main macro. I do not configure the thread spawning in any way; the default behaviour of the macro is used.
I found two solutions to this:
- Put the variable in a Box and then run leak() on it and give it a static lifetime.
- Put the variable in an Arc, and give each thread a clone of the Arc.
Initially I thought using an Arc would be preferable as leaking sounded bad in my head, however now that I think about it I imagine Arc would have unnecessary overhead as from what I understand it needs to do more than a variable leaked from a Box.
Is this the case? Would using an Arc here introduce unnecessary overhead? And, considering my situation, is there any reason I might want to use an Arc? I'm looking to introduce as little overhead as possible as I want minimal latency for operations.