Hi everyone! First of all - sorry for my english
Always used to write on C, but now i have to do kind of homework using rust and it's really hard to understand some things. The algorithm is:
ask dimensions of matrix
put random numbers there
create 2 threads, and give each of them to count a half of rows for answer matrix
print answer
the 3rd step looks like that:
let fthr = thread::spawn( move || {
for z in 0..half {
for i in 0..d{
for j in 0..a{
resultM[z][i]+=(firstM[z][j])*(secondM[j][i]);} } }
});
let sthr = thread::spawn(move || {
for z in half..a {
for i in 0..d{
for j in 0..a{
resultM[z][i]+=(firstM[z][j])*(secondM[j][i]);} } }
});
And of course i got error "used moved value" for second thread and both 3 matrix. But what should i use to share them? channels? mutex? Just don't know what is the right way to solve this problem
Thanks!
std::thread::spawn does not support scoped threads or in other words, does not support threads that borrow from an outer scope, like you'd need to do to use split_at_mut.
Many libraries offer scoped threads, including rayon which has a simple join(a, b) function that splits work into two parallel parts.
Using std::thread::spawn while still sharing (dynamic, as in non-static) memory requires using the reference counting smart pointer Arc for shared ownership.