Seem to be going round in circles again. I pass lots of refs around using Arc but these are not my objects and don't need to be mutable. I have one user type instance that I can't really avoid passing around and it has to be mutable but which ever way I try to do this I get stuck.
As I understand it I should be able to use Rc/RefCell to do this. There is a lot of code so I will post minimal but its on GitHub if the full files are useful.
This is the field in the local struct.
pub i_cc_out : Rc<RefCell<protocol::cc_out::CCDataMutex>>,
and the initialisation.
let i_cc_out = Rc::new(RefCell::new(protocol::cc_out::CCDataMutex::new()));
At the moment I am only trying to pass this to one module which is a thread. It will be passed to at least one other module (actually the GUI on the main thread).
The thread is started from the local module
// Start the UDP writer thread
opt_writer_join_handle = Some(
udp::udp_writer::writer_start(w_r.clone(),
arc3, arc5,
rb_audio.clone(), audio_cond.clone(), &mut i_cc_out.borrow_mut()));
No compile errors showing at this point.
In the writer thread module
//==================================================================================
// Thread startup
pub fn writer_start(
receiver : crossbeam_channel::Receiver<messages::WriterMsg>,
p_sock : Arc<socket2::Socket>,
p_addr : Arc<socket2::SockAddr>,
rb_audio : Arc<ringb::SyncByteRingBuf>,
audio_cond : Arc<(Mutex<bool>, Condvar)>,
i_cc_out : &mut RefMut<protocol::cc_out::CCDataMutex>) -> thread::JoinHandle<()> {
let join_handle = thread::spawn( move || {
writer_run(receiver, &p_sock, &p_addr, &rb_audio, &audio_cond, &mut i_cc_out);
});
return join_handle; //join_handle;
}
fn writer_run(
receiver : crossbeam_channel::Receiver<messages::WriterMsg>,
p_sock : &socket2::Socket,
p_addr : &socket2::SockAddr,
rb_audio : &ringb::SyncByteRingBuf,
audio_cond : &(Mutex<bool>, Condvar),
i_cc_out : &mut protocol::cc_out::CCDataMutex) {
println!("UDP Writer running");
// Instantiate the runtime object
let mut i_writer = UDPWData::new(receiver, p_sock, p_addr, rb_audio, audio_cond, i_cc_out);
// Exits when the reader loop exits
i_writer.writer_run();
println!("UDP Writer exiting");
thread::sleep(Duration::from_millis(1000));
}
The error occurs when trying to spawn the thread.
error[E0277]: NonNull<CCDataMutex>
cannot be sent between threads safely
--> src\app\udp\udp_writer.rs:175:23
|
175 | let join_handle = thread::spawn( move || {
| ^^^^^^^^^^^^^ NonNull<CCDataMutex>
cannot be sent between threads safely
|
= help: within RefMut<'_, CCDataMutex>
, the trait Send
is not implemented for NonNull<CCDataMutex>
= note: required because it appears within the type RefMut<'_, CCDataMutex>
= note: required because of the requirements on the impl of Send
for &mut RefMut<'_, CCDataMutex>
note: required because it's used within this closure
--> src\app\udp\udp_writer.rs:175:39
|
175 | let join_handle = thread::spawn( move || {
| ^^^^^^^
note: required by a bound in spawn
--> C:\Users\User.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\std\src\thread\mod.rs:653:8
|
653 | F: Send + 'static,
| ^^^^ required by this bound in spawn
error[E0277]: std::cell::Cell<isize>
cannot be shared between threads safely
--> src\app\udp\udp_writer.rs:175:23
|
175 | let join_handle = thread::spawn( move || {
| ^^^^^^^^^^^^^ std::cell::Cell<isize>
cannot be shared between threads safely
|
= help: the trait Sync
is not implemented for std::cell::Cell<isize>
= note: required because of the requirements on the impl of Send
for &std::cell::Cell<isize>
= note: required because it appears within the type cell::BorrowRefMut<'_>
= note: required because it appears within the type RefMut<'_, CCDataMutex>
= note: required because of the requirements on the impl of Send
for &mut RefMut<'_, CCDataMutex>
note: required because it's used within this closure
--> src\app\udp\udp_writer.rs:175:39
|
175 | let join_handle = thread::spawn( move || {
| ^^^^^^^
note: required by a bound in spawn
--> C:\Users\User.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\std\src\thread\mod.rs:653:8
|
653 | F: Send + 'static,
| ^^^^ required by this bound in spawn