# Dealing with multiple threads

**URL:** <https://users.rust-lang.org/t/dealing-with-multiple-threads/60993>\
**Category:** help\
**Created:** [June 10, 2021, 10:13pm UTC](https://users.rust-lang.org/t/dealing-with-multiple-threads/60993 "2021-06-10T22:13:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![woifefioewfj23po](https://avatars.discourse-cdn.com/v4/letter/w/898d66/32.png) [@woifefioewfj23po](https://users.rust-lang.org/u/woifefioewfj23po)\
**Post date:** [June 10, 2021, 10:13pm UTC](https://users.rust-lang.org/t/dealing-with-multiple-threads/60993/1 "2021-06-10T22:13:59Z")

</div>

Hey there, so I have a small program with the following setup:  
It has two threads, the main one where most of the action happens, and a "disposable" thread which I use to run a constant loop without hanging up the program.

Before needing the later thread, I was using `Rc<RefCell<Struct>>` to be able to use the methods of a struct I couldn't derive `Clone` to inside static closures (event handlers for FLTK widgets). However I later switched to `Arc<Mutex<Struct>>` since I needed that multithread capability.

So far I've been able to get an external "stop signal" working for the loop, save for the delay that occurs after the first time I close. The issue lies in the fact that once I call a method of the struct outside of the separate thread, the clone done via `Arc::clone()` stops responding.

Snippet of the code:

```rust
let var = Arc::new(Mutex::new(var));
    // Choice is a "HoldBrowser" from FLTK
    choice.set_callback({
        let loop_is_active = Arc::new(Mutex::new(false));
        move |browser| match choices_list[(browser.value() - 1) as usize] {
            "Threaded" => {
                *loop_is_active.lock().unwrap() = true;
                let should_loop = Arc::clone(&loop_is_active);
                let clone_var = Arc::clone(&var);
                thread::spawn(move || {
                    while *should_loop.lock().unwrap() {
                        thread::sleep(time::Duration::from_millis(200));
                        clone_var.lock().unwrap().do_something(2);
                    }
                });
            }
            "NotThreaded" => {
                *loop_is_active.lock().unwrap() = false;
                //The moment this case is reached and the instruction bellow is executed, clone_var inside "Threaded" stops working. Any other use outside of the separate thread works fine.
                var.lock().unwrap().do_something_else(0);
            }
            _ => {}
        }
    });

```

Code abominations aside, can someone help me figure out why this is happening?

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/steffahn/32/47569_2.png) [@steffahn](https://users.rust-lang.org/u/steffahn)\
**Post date:** [June 10, 2021, 10:39pm UTC](https://users.rust-lang.org/t/dealing-with-multiple-threads/60993/2 "2021-06-10T22:39:35Z")

</div>

Are you just referring to the fact that _while_ `do_something_else` is running, the other thread isn't able to execute `do_something`? (That would be because you locked the mutex for the duration of that call.) If it's something else, you'll need to tell us more details; it's very hard to tell _what exactly_ the behavior is that you're observing and what you'd be expecting to happen instead / what you're aiming to achieve.

---

<div class="post-metadata">

**Author:** ![woifefioewfj23po](https://avatars.discourse-cdn.com/v4/letter/w/898d66/32.png) [@woifefioewfj23po](https://users.rust-lang.org/u/woifefioewfj23po)\
**Post date:** [June 10, 2021, 10:43pm UTC](https://users.rust-lang.org/t/dealing-with-multiple-threads/60993/3 "2021-06-10T22:43:46Z")

</div>

Ah no, rather after running `do_something_else`, `do_something` does not work anymore afterwards on any call to it inside the separate thread. As for expecting, well, its just it not suddenly not working.

Apologies if I seem lost with the responses (_kinda am_), first timer on asking in forums.  
😅

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/steffahn/32/47569_2.png) [@steffahn](https://users.rust-lang.org/u/steffahn)\
**Post date:** [June 10, 2021, 10:53pm UTC](https://users.rust-lang.org/t/dealing-with-multiple-threads/60993/4 "2021-06-10T22:53:46Z")

</div>

Hmm, you're setting the `loop_is_active` value to `false` which is going to stop the loop from running. Is that just what's happening? Or are you expecting the loop _not_ to stop for some reason?

~~Wait, nevermind, each call crates its own independent `loop_is_active` value, right?~~ No it doesn't, my point above still stands.

---

<div class="post-metadata">

**Author:** ![woifefioewfj23po](https://avatars.discourse-cdn.com/v4/letter/w/898d66/32.png) [@woifefioewfj23po](https://users.rust-lang.org/u/woifefioewfj23po)\
**Post date:** [June 10, 2021, 10:59pm UTC](https://users.rust-lang.org/t/dealing-with-multiple-threads/60993/5 "2021-06-10T22:59:48Z")

</div>

Not that, the calling order is

- Threaded
- NotThreaded
- Threaded

The loop stops the first time as expected, on the second "Threaded" the loop starts again, but `do_something()` inside the loop does not work anymore, the rest of the code does (Including stopping the loop again).

I can call `do_something()` as I please as long as all calls are done inside the (muliple) separate threads, the moment "NotThreaded" is called all methods for clone\_var stop working when used inside the loop thread.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/steffahn/32/47569_2.png) [@steffahn](https://users.rust-lang.org/u/steffahn)\
**Post date:** [June 10, 2021, 11:04pm UTC](https://users.rust-lang.org/t/dealing-with-multiple-threads/60993/6 "2021-06-10T23:04:12Z")

</div>

> [@woifefioewfj23po](#):
>
> Not that, the calling order is
> 
> - Threaded
> - NotThreaded
> - Threaded
> 
> The loop stops the first time as expected, on the second "Threaded" the loop starts again, but `do_something()` inside the loop does not work anymore

Ah, now the question makes a lot more sense to me.

> [@woifefioewfj23po](#):
>
> all methods for clone\_var stop working

Can you describe more precisely what exactly "stop working" means? Like... if you have a print statement inside of the definition of `do_something` doesn't it work? But putting a print statement inside of the loop of the looping thread directly _does_ still get executed (repeatedly)?

_Edit: I'll have to leave for today, I hope someone else can help you in the meantime if your problem persists ^^_

---

<div class="post-metadata">

**Author:** ![woifefioewfj23po](https://avatars.discourse-cdn.com/v4/letter/w/898d66/32.png) [@woifefioewfj23po](https://users.rust-lang.org/u/woifefioewfj23po)\
**Post date:** [June 10, 2021, 11:18pm UTC](https://users.rust-lang.org/t/dealing-with-multiple-threads/60993/7 "2021-06-10T23:18:59Z")

</div>

Ok so I do not know what I did, but I got it working, I- uh-...

_brain.exe stopped working_ Ah, the wonders of coding.

Anyways, guess that solves that? Thanks for the help 😂.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [September 8, 2021, 11:19pm UTC](https://users.rust-lang.org/t/dealing-with-multiple-threads/60993/8 "2021-09-08T23:19:44Z")

</div>

Closed on September 8, 2021, 11:19pm UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
