#Closures

1 messages · Page 1 of 1 (latest)

wind apex
#

my_string is being mutated, which means my_closure is being mutated

#

recall that closures are just anonymous structs

#

imagine something like ```rust
struct Closure<'a> {
capture: &'a mut String,
}

impl Closure<'_> {
fn call(&self) {
println!("{:?}", self.capture.pop())
}
}

#

that obviously can't work because it's being borrowed as &self

#

yup

#

its pretty easy once you get it

#

it probably spawns some of them before the first one has actually started running code

#

you dont have any guarantees about execution order unless you join on the handle or use barriers etc

past frost
#

You cant predict that. If you spawn a new thread a completly new execution context branching from the main thread. You can imagine throwing marbles down a slope. You can throw them one after the other, but it depends on the marble when it finishes. You can ofc wait on one marble to reach the bottom before throwing the next (by joining the thread again)

#

it uses OS specific tools to check in a busy loop or asks the os to be woken up again when the thread finishes

wind apex
#

join() will block until the closure of spawn returns

past frost
#

you can see that the thread never terminates. so the join never really finishes

fn main() {
    let handle = std::thread::spawn(||{ loop {};  42 });

    let y = handle.join().unwrap();

    println!("{y}");
}```
spring jewel
#

you can use godbolt to see the asm

#

?godbolt ```rs
#[no_mangle]
pub fn foo() -> u64 {
(0u64..1000u64).sum()
}

signal walrusBOT
#
foo:
        mov     eax, 499500
        ret
#

Missing code block. Please use the following markdown:
`code here`
or

```rust
code here
```
#
warning: function `main` is never used
 --> <source>:1:4
  |
1 | fn main() {
  |    ^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted
```No output. Consider adding `#[no_mangle]` before your functions.
spring jewel
#

it doesn't do main(), only pub fns

wind apex
#

i dont think assembly will help much here. it will just call some platform specific function for spawning threads