crossbeam | Tools for concurrent programming in Rust | Reactive Programming library
kandi X-RAY | crossbeam Summary
kandi X-RAY | crossbeam Summary
This crate provides a set of tools for concurrent programming:.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of crossbeam
crossbeam Key Features
crossbeam Examples and Code Snippets
Community Discussions
Trending Discussions on crossbeam
QUESTION
I am trying to pass enum type data to a thread using channels in Rust, but I keep getting the following error when I run the program (it compiles fine),
thread 'thread 'main' panicked at '' panicked at 'attempted to leave type
internal::Local
uninitialized, which is invalid. Attempted to leave typeinternal::Local
uninitialized, which is invalid'
Here is the code snippet,
...ANSWER
Answered 2022-Mar-27 at 05:55In short: this error shows the unsoundness in an old crossbeam
version, you must update it for your code to work.
If we run the code from OP with RUST_BACKTRACE=1
, we'll see the following:
QUESTION
I have the following code that is using a bounded channel with capacity (20) less than the total amount of data (32) that I want to send via a crossbeam channel. My goal is to use multiple sender threads (8) and a certain amount (4) of numbers each to a single receiver via a crossbeam channel and for all of this to happen in parallel to optimize efficiency. This is just a small prototype of a bigger problem that I'm trying to solve. However, the code that I have is causing the program to get stuck endlessly and never exit/timeout. I also know why it's happening - r.iter()
blocks until the sender is dropped which only happens outside of the scope. I tried various approaches that didn't work:
Cloning sender withing each thread and then dropping them (as you can see in the comments)
Having the receiver code outside the scope, but that was only letting the final vector contain 20 length instead of the desired 32.
ANSWER
Answered 2022-Mar-09 at 19:21This is happening because s
isn't dropped until the scope ends, but the scope won't end until all threads have exited, and the thread calling r.iter()
won't exit until s
is dropped. This is a classic deadlock scenario.
You need to drop s
inside the scope, but you can only do that once the sender threads have all exited, so you can't drop(s);
in the scope the way this is currently written.
The simplest way around this is to clone s
for each sender thread and move the clone into the thread's closure, then drop s
in the main scope afterwards.
QUESTION
I try to process an array with multiple worker threads, just like Rayon's par_iter()
function, but i want to have a mutable reference target
, one for each thread.
I have a simple function like this
...ANSWER
Answered 2022-Jan-31 at 11:23The playground example to which you have linked is using the 2021 edition of Rust, which allows disjoint capture in closures (and hence the closure for each thread captures only target
rather than the entire t
slice). If you switch to the 2018 edition, it yields the same error as in your question.
Specify that you require the 2021 edition in your Cargo.toml
(requires rustc >= 1.56.0).
QUESTION
Please is there a way for sender to delete pending messages sent to mpsc channel (or crossbeam-channel or equivalent) which have not been consumed by the receiver yet? Something like sender.deleteAllPending()
.
ANSWER
Answered 2021-Dec-19 at 07:00You can't do that with mspc which is "single consumer".
With crossbeam, you can have multiple receivers. So you can clone and keep a receiver "sender side", so you can drain it.
QUESTION
I am practicing rust and decided to create a Matrix ops/factorization project.
Basically I want to be able to process the underlying vector in multiple threads. Since I will be providing each thread non-overlapping indexes (which may or may not be contiguous) and the threads will be joined before the end of whatever function created them, there is no need for a lock /synchronization.
I know that there are several crates that can do this, but I would like to know if there is a relatively idiomatic crate-free way to implement it on my own.
The best I could come up with is (simplified the code a bit):
...ANSWER
Answered 2021-Sep-26 at 15:46This API is unsound. Since there is no lifetime annotation binding SliceTest
and SubSlice
to the MainStruct
, they can be preserved after the data has been destroyed and if used would result in use-after-free errors.
Its easy to make it safe though; you can use .iter_mut()
to get distinct mutable references to your elements:
QUESTION
I'm trying to send a "view" of a read-only data to another thread for processing. Basically the main thread does work, and continuously updates a set of data. Whenever an update occurs, the main thread should send the updated data down to other threads where they will process it in a read-only manner. I do not want to copy the data as it may be very large. (The main thread also keeps a "cache" of the data in-memory anyway.)
I can achieve this with Arc>
, where T
being my data structure.
However, there is nothing stopping the side threads updating the data. The side threads can simply call lock()
and the write to the data.
My question is there something similar to RwLock
where the owner/creator of it has the only write access, but all other instances have read-only access? This way I will have compile time checking of any logic bugs that may occur via side threads accidentally updating data.
Regarding these questions:
The above questions suggest solving it with Arc>
or Arc>
which is all fine. But it still doesn't give compile time enforcement of only one writer.
Additionally: crossbeam
or rayon
's scoped threads don't help here as I want my side threads to outlive my main thread.
ANSWER
Answered 2021-Aug-24 at 13:29You can create a wrapper type over an Arc>
that only exposes cloning via a read only wrapper:
QUESTION
I am trying to make a prime sieve in Rust.
I need several mutable references to the same element of the array, not mutable references to different parts of the array.
For the way this works, I know data races are not a relevant problem, so multiple mutable references are acceptable, but the Rust compiler does not accept my unsafe code.
I am using crossbeam 0.8.0.
...ANSWER
Answered 2021-May-26 at 01:15Rust does not allow that in its model. You want AtomicBool
with relaxed ordering.
This is a somewhat common edge case in most concurrency models - if you have two non-atomic writes of the same value to one location, is that well-defined? In Rust (and C++) it is not, and you need to explicitly use atomic.
On any platform you care about, the atomic bool store with relaxed ordering will have no impact.
For a reason why this matters, consider:
QUESTION
I started coding with rust lately and i'm loving it. I'm coding on a project where I want to "wrap" a C-API. In one case I have to define callbacks in Rust, which C can call. I let bindgen created the callbacks. Since the code need to run somewhat asynchronous, i'm using tokio for that.
What I want to archieve:I create the the main function as tokio::main. In the main function i create 2 async tasks, one listens for channels, the other triggers the message queue in the C-API. If messages are available I want to send them via a channel on the callback function, so i can receive the message on the task, where i am listening for events. Later I want to send these messages via SSE or a GraphQL subscription to several clients.
I can't change the C-Callbacks as they need to be passable to the C-API, and I have to use callbacks, otherwise i dont get the messages.
My latest approach looks simplified like that:
...ANSWER
Answered 2020-Dec-26 at 12:38If you make the following changes to your first example, it should work:
- Replace
tokio::sync::Mutex
withstd::sync::Mutex
so you don't have to usetry_lock
in the callback. - Do not store the receiver in the mutex, only the sender.
- In the callback, either use an unbounded channel, or make sure to release the lock before sending.
- Run the blocking C code on a dedicated thread with
std::thread::spawn
rather than intokio::spawn
. (why?)
To not store the receiver in the mutex, you can do this:
QUESTION
Why does this worK:
...ANSWER
Answered 2020-Dec-25 at 20:51The problem is that rust cannot correctly infer the type of the closure (without going into detail let's say that the compiler is not soo good in type inference betwen closures).
In your case you just had to add a type anotation on all the closure arguments, for instance
QUESTION
I have two slices that are passed from another method:
...ANSWER
Answered 2020-Dec-07 at 10:56How to do this in Rust? Resorting to unsafe is okay.
If you need two threads to mutate the same data, without locks, then you must use unsafe
. It is Undefined Behaviour for two mutable references (&mut
) to the same data to even exist, so you would need to access the data via *mut
raw pointers, while being extremely careful to avoid data races.
However, depending on your algorithm, you may be able to avoid unsafe
by creating non-overlapping &mut
references with split_at_mut
.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install crossbeam
Rust is installed and managed by the rustup tool. Rust has a 6-week rapid release process and supports a great number of platforms, so there are many builds of Rust available at any time. Please refer rust-lang.org for more information.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page