Skip to content

std::sync::Arc odd behavior #15622

@ghigt

Description

@ghigt

First, sorry if it is not a bug but the normal behavior.

I can't seem to understand why this two programs give a different answer (try):

use std::sync::Arc;

fn main() {
    let numbers = vec![1i, 2i, 3i];
    let numbers = Arc::new(numbers);

    for num in range(0u, 3) {
        let (tx, rx)  = channel();
        tx.send(numbers.clone());

        spawn(proc() {
            let numbers = rx.recv();
            println!("{:d}", *numbers.get(num as uint));
        })
    }
}

Gives this result: 3 2 1
Changing the numbers declaration by (try):

let numbers = Arc::new(vec![1i, 2i, 3i]);

Gives this result: 3 1 2
Also changing the order like that (try):

let numbers = Arc::new(vec![3i, 2i, 1i]);

Give this result: 1 2 3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions