Could use some help from Rust Twitter on this one: I want to provide each thread with a Vec in a thread-local. Then from another thread I want to periodically take ownership of the thread-local vecs and swap them with empty ones. Is there a good pattern/lib for this?
I'm thinking I could make this work with an Arc<Mutex<Vec>> stored in the thread-local, and sending a clone over the the driver thread. But it feels like this should be doable using a compare-and-swap of sorts, but unsure how to make this work correctly. This should be common?
-
-
Does it have to be in TLS? Which side controls the swapping, worker threads or the collecting thread?
-
Good questions! We need to instantiate one instance per thread, so thread-local seemed like the best match. Collection thread does the swapping.
- 7 more replies
New conversation -
-
-
Are you making hazard pointers >.> My first question is what are you trying to achieve? The problem will be the monitor thread, and new thread creations. The monitor can't steal thread locals because it has a diff pthread key, unless you make it a global or ref in another struct
-
The next issue is a shared struct with refs, may realloc and that will break any pointers.
- 6 more replies
New conversation -
-
-
threads are producers, and you want a kind of asynchronous / batching / chunking collector, to avoid locking on every item? something like bbqueue? swapping leases on the two regions between the threads?
-
"swapping leases" -- that sounds about what I want to be doing. Hmm, yeah perhaps?
End of conversation
New conversation -
-
-
If you don't actually have to "swap" a recycled Vec, I'd allocate a new Vec in the thread-local, and send the previous over a channel to the accumulator thread. I could be misreading the requirements though!
-
Yeah, it seems like the collector should be able to receive the Vecs, and the workers could allocate the new Vec
End of conversation
New conversation -
-
-
I think you could have an vector of atomic pointers that has a pointer for each thread and do a compare-and-swap. But they would need no be padded to avoid false sharing.
Thanks. Twitter will use this to make your timeline better. UndoUndo
-
-
-
AtomicPtr is specially for that
Thanks. Twitter will use this to make your timeline better. UndoUndo
-
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.