What is the proper way to atomically update a vector or a slice that overlaps between multiple threads?











up vote
0
down vote

favorite












I want to some work on a vector shared by multiple threads but I don't want to use a Mutex because it is not wait-free.



The code below is written as I would in C.



#![feature(core_intrinsics, ptr_internals)]

use std::intrinsics::atomic_xadd_rel;
use std::ptr::Unique;
use std::thread::spawn;

fn main() {
let mut data = [0; 8];
let mut pool = Vec::with_capacity(8);
for index in 0..8 {
let data_ptr = Unique::new(data.as_mut_ptr());
pool.push(spawn(move || {
println!("Thread {} -> {}", index, unsafe {
atomic_xadd_rel(
data_ptr
.unwrap()
.as_ptr()
.add(if index % 2 != 0 { index - 1 } else { index }),
1,
)
});
}));
}
for work in pool {
work.join().unwrap();
}
println!("Data {:?}", data);
}


I've also written the code using only the stable API:



use std::iter::repeat_with;
use std::sync::atomic::{AtomicUsize, Ordering::*};
use std::sync::Arc;
use std::thread::spawn;

fn main() {
let data = Arc::new(
repeat_with(|| AtomicUsize::new(0))
.take(8)
.collect::<Vec<_>>(),
);
let mut pool = Vec::with_capacity(8);
for index in 0..8 {
let data_clone = data.clone();
pool.push(spawn(move || {
let offset = index - (index % 2 != 0) as usize;
println!(
"Thread {} -> {}",
index,
data_clone[offset].fetch_add(1, Relaxed)
);
}));
}
for work in pool {
work.join().unwrap();
}
println!("Data {:?}", data);
}


This code returns



Thread 0 -> 0
Thread 1 -> 1
Thread 3 -> 0
Thread 5 -> 1
Thread 7 -> 1
Thread 2 -> 1
Thread 6 -> 0
Thread 4 -> 0
Data [2, 0, 2, 0, 2, 0, 2, 0]


Is there is a proper way to do this in Rust?



I do not think this is a duplicate of How do I pass disjoint slices from a vector to different threads? because my vector / slice elements overlap between threads. In my sample, each odd index of the slice is incremented twice by two different threads.










share|improve this question




















  • 1




    Possible duplicate of How do I pass disjoint slices from a vector to different threads?
    – Lucretiel
    Nov 22 at 0:17















up vote
0
down vote

favorite












I want to some work on a vector shared by multiple threads but I don't want to use a Mutex because it is not wait-free.



The code below is written as I would in C.



#![feature(core_intrinsics, ptr_internals)]

use std::intrinsics::atomic_xadd_rel;
use std::ptr::Unique;
use std::thread::spawn;

fn main() {
let mut data = [0; 8];
let mut pool = Vec::with_capacity(8);
for index in 0..8 {
let data_ptr = Unique::new(data.as_mut_ptr());
pool.push(spawn(move || {
println!("Thread {} -> {}", index, unsafe {
atomic_xadd_rel(
data_ptr
.unwrap()
.as_ptr()
.add(if index % 2 != 0 { index - 1 } else { index }),
1,
)
});
}));
}
for work in pool {
work.join().unwrap();
}
println!("Data {:?}", data);
}


I've also written the code using only the stable API:



use std::iter::repeat_with;
use std::sync::atomic::{AtomicUsize, Ordering::*};
use std::sync::Arc;
use std::thread::spawn;

fn main() {
let data = Arc::new(
repeat_with(|| AtomicUsize::new(0))
.take(8)
.collect::<Vec<_>>(),
);
let mut pool = Vec::with_capacity(8);
for index in 0..8 {
let data_clone = data.clone();
pool.push(spawn(move || {
let offset = index - (index % 2 != 0) as usize;
println!(
"Thread {} -> {}",
index,
data_clone[offset].fetch_add(1, Relaxed)
);
}));
}
for work in pool {
work.join().unwrap();
}
println!("Data {:?}", data);
}


This code returns



Thread 0 -> 0
Thread 1 -> 1
Thread 3 -> 0
Thread 5 -> 1
Thread 7 -> 1
Thread 2 -> 1
Thread 6 -> 0
Thread 4 -> 0
Data [2, 0, 2, 0, 2, 0, 2, 0]


Is there is a proper way to do this in Rust?



I do not think this is a duplicate of How do I pass disjoint slices from a vector to different threads? because my vector / slice elements overlap between threads. In my sample, each odd index of the slice is incremented twice by two different threads.










share|improve this question




















  • 1




    Possible duplicate of How do I pass disjoint slices from a vector to different threads?
    – Lucretiel
    Nov 22 at 0:17













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to some work on a vector shared by multiple threads but I don't want to use a Mutex because it is not wait-free.



The code below is written as I would in C.



#![feature(core_intrinsics, ptr_internals)]

use std::intrinsics::atomic_xadd_rel;
use std::ptr::Unique;
use std::thread::spawn;

fn main() {
let mut data = [0; 8];
let mut pool = Vec::with_capacity(8);
for index in 0..8 {
let data_ptr = Unique::new(data.as_mut_ptr());
pool.push(spawn(move || {
println!("Thread {} -> {}", index, unsafe {
atomic_xadd_rel(
data_ptr
.unwrap()
.as_ptr()
.add(if index % 2 != 0 { index - 1 } else { index }),
1,
)
});
}));
}
for work in pool {
work.join().unwrap();
}
println!("Data {:?}", data);
}


I've also written the code using only the stable API:



use std::iter::repeat_with;
use std::sync::atomic::{AtomicUsize, Ordering::*};
use std::sync::Arc;
use std::thread::spawn;

fn main() {
let data = Arc::new(
repeat_with(|| AtomicUsize::new(0))
.take(8)
.collect::<Vec<_>>(),
);
let mut pool = Vec::with_capacity(8);
for index in 0..8 {
let data_clone = data.clone();
pool.push(spawn(move || {
let offset = index - (index % 2 != 0) as usize;
println!(
"Thread {} -> {}",
index,
data_clone[offset].fetch_add(1, Relaxed)
);
}));
}
for work in pool {
work.join().unwrap();
}
println!("Data {:?}", data);
}


This code returns



Thread 0 -> 0
Thread 1 -> 1
Thread 3 -> 0
Thread 5 -> 1
Thread 7 -> 1
Thread 2 -> 1
Thread 6 -> 0
Thread 4 -> 0
Data [2, 0, 2, 0, 2, 0, 2, 0]


Is there is a proper way to do this in Rust?



I do not think this is a duplicate of How do I pass disjoint slices from a vector to different threads? because my vector / slice elements overlap between threads. In my sample, each odd index of the slice is incremented twice by two different threads.










share|improve this question















I want to some work on a vector shared by multiple threads but I don't want to use a Mutex because it is not wait-free.



The code below is written as I would in C.



#![feature(core_intrinsics, ptr_internals)]

use std::intrinsics::atomic_xadd_rel;
use std::ptr::Unique;
use std::thread::spawn;

fn main() {
let mut data = [0; 8];
let mut pool = Vec::with_capacity(8);
for index in 0..8 {
let data_ptr = Unique::new(data.as_mut_ptr());
pool.push(spawn(move || {
println!("Thread {} -> {}", index, unsafe {
atomic_xadd_rel(
data_ptr
.unwrap()
.as_ptr()
.add(if index % 2 != 0 { index - 1 } else { index }),
1,
)
});
}));
}
for work in pool {
work.join().unwrap();
}
println!("Data {:?}", data);
}


I've also written the code using only the stable API:



use std::iter::repeat_with;
use std::sync::atomic::{AtomicUsize, Ordering::*};
use std::sync::Arc;
use std::thread::spawn;

fn main() {
let data = Arc::new(
repeat_with(|| AtomicUsize::new(0))
.take(8)
.collect::<Vec<_>>(),
);
let mut pool = Vec::with_capacity(8);
for index in 0..8 {
let data_clone = data.clone();
pool.push(spawn(move || {
let offset = index - (index % 2 != 0) as usize;
println!(
"Thread {} -> {}",
index,
data_clone[offset].fetch_add(1, Relaxed)
);
}));
}
for work in pool {
work.join().unwrap();
}
println!("Data {:?}", data);
}


This code returns



Thread 0 -> 0
Thread 1 -> 1
Thread 3 -> 0
Thread 5 -> 1
Thread 7 -> 1
Thread 2 -> 1
Thread 6 -> 0
Thread 4 -> 0
Data [2, 0, 2, 0, 2, 0, 2, 0]


Is there is a proper way to do this in Rust?



I do not think this is a duplicate of How do I pass disjoint slices from a vector to different threads? because my vector / slice elements overlap between threads. In my sample, each odd index of the slice is incremented twice by two different threads.







rust






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 21:15









Shepmaster

146k11279413




146k11279413










asked Nov 21 at 23:48









The_Server201

12




12








  • 1




    Possible duplicate of How do I pass disjoint slices from a vector to different threads?
    – Lucretiel
    Nov 22 at 0:17














  • 1




    Possible duplicate of How do I pass disjoint slices from a vector to different threads?
    – Lucretiel
    Nov 22 at 0:17








1




1




Possible duplicate of How do I pass disjoint slices from a vector to different threads?
– Lucretiel
Nov 22 at 0:17




Possible duplicate of How do I pass disjoint slices from a vector to different threads?
– Lucretiel
Nov 22 at 0:17












1 Answer
1






active

oldest

votes

















up vote
1
down vote













Assuming that each thread has unique access to a particular element or sub-slice of your vector, this would be a case to use split_at (or one of the similar functions). split_at splits a mutable slice into two independent mutable slices; you can call it multiple times to split your slice into the correct number of segments, and pass each sub-slice to a separate thread.



The best way to pass the sub-slices to a thread would be to use something like the scoped threads in crossbeam.






share|improve this answer





















  • So, you agree this is a duplicate of the proposed duplicate?
    – Shepmaster
    Nov 22 at 0:13












  • Oh, I didn't see that. Their solution is different, but yes,
    – Lucretiel
    Nov 22 at 0:17










  • The OP has edited the question such that this answer is no longer valid — split_at is impossible to call here.
    – Shepmaster
    Nov 22 at 21:17











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422033%2fwhat-is-the-proper-way-to-atomically-update-a-vector-or-a-slice-that-overlaps-be%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













Assuming that each thread has unique access to a particular element or sub-slice of your vector, this would be a case to use split_at (or one of the similar functions). split_at splits a mutable slice into two independent mutable slices; you can call it multiple times to split your slice into the correct number of segments, and pass each sub-slice to a separate thread.



The best way to pass the sub-slices to a thread would be to use something like the scoped threads in crossbeam.






share|improve this answer





















  • So, you agree this is a duplicate of the proposed duplicate?
    – Shepmaster
    Nov 22 at 0:13












  • Oh, I didn't see that. Their solution is different, but yes,
    – Lucretiel
    Nov 22 at 0:17










  • The OP has edited the question such that this answer is no longer valid — split_at is impossible to call here.
    – Shepmaster
    Nov 22 at 21:17















up vote
1
down vote













Assuming that each thread has unique access to a particular element or sub-slice of your vector, this would be a case to use split_at (or one of the similar functions). split_at splits a mutable slice into two independent mutable slices; you can call it multiple times to split your slice into the correct number of segments, and pass each sub-slice to a separate thread.



The best way to pass the sub-slices to a thread would be to use something like the scoped threads in crossbeam.






share|improve this answer





















  • So, you agree this is a duplicate of the proposed duplicate?
    – Shepmaster
    Nov 22 at 0:13












  • Oh, I didn't see that. Their solution is different, but yes,
    – Lucretiel
    Nov 22 at 0:17










  • The OP has edited the question such that this answer is no longer valid — split_at is impossible to call here.
    – Shepmaster
    Nov 22 at 21:17













up vote
1
down vote










up vote
1
down vote









Assuming that each thread has unique access to a particular element or sub-slice of your vector, this would be a case to use split_at (or one of the similar functions). split_at splits a mutable slice into two independent mutable slices; you can call it multiple times to split your slice into the correct number of segments, and pass each sub-slice to a separate thread.



The best way to pass the sub-slices to a thread would be to use something like the scoped threads in crossbeam.






share|improve this answer












Assuming that each thread has unique access to a particular element or sub-slice of your vector, this would be a case to use split_at (or one of the similar functions). split_at splits a mutable slice into two independent mutable slices; you can call it multiple times to split your slice into the correct number of segments, and pass each sub-slice to a separate thread.



The best way to pass the sub-slices to a thread would be to use something like the scoped threads in crossbeam.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 at 0:11









Lucretiel

793824




793824












  • So, you agree this is a duplicate of the proposed duplicate?
    – Shepmaster
    Nov 22 at 0:13












  • Oh, I didn't see that. Their solution is different, but yes,
    – Lucretiel
    Nov 22 at 0:17










  • The OP has edited the question such that this answer is no longer valid — split_at is impossible to call here.
    – Shepmaster
    Nov 22 at 21:17


















  • So, you agree this is a duplicate of the proposed duplicate?
    – Shepmaster
    Nov 22 at 0:13












  • Oh, I didn't see that. Their solution is different, but yes,
    – Lucretiel
    Nov 22 at 0:17










  • The OP has edited the question such that this answer is no longer valid — split_at is impossible to call here.
    – Shepmaster
    Nov 22 at 21:17
















So, you agree this is a duplicate of the proposed duplicate?
– Shepmaster
Nov 22 at 0:13






So, you agree this is a duplicate of the proposed duplicate?
– Shepmaster
Nov 22 at 0:13














Oh, I didn't see that. Their solution is different, but yes,
– Lucretiel
Nov 22 at 0:17




Oh, I didn't see that. Their solution is different, but yes,
– Lucretiel
Nov 22 at 0:17












The OP has edited the question such that this answer is no longer valid — split_at is impossible to call here.
– Shepmaster
Nov 22 at 21:17




The OP has edited the question such that this answer is no longer valid — split_at is impossible to call here.
– Shepmaster
Nov 22 at 21:17


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422033%2fwhat-is-the-proper-way-to-atomically-update-a-vector-or-a-slice-that-overlaps-be%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...

Berounka

I want to find a topological embedding $f : X rightarrow Y$ and $g: Y rightarrow X$, yet $X$ is not...