Skip to content

Null Pointer Error when working with Mutex #18351

@kiwistrongis

Description

@kiwistrongis

Hi there :). I had a little issue when working with a mutex. The following code results in a null pointer error:

//standard library imports
use std::rt::thread::Thread;
use std::sync::{ Arc, Mutex};

fn main(){
    //make mutex
    let done = Arc::new( Mutex::new( false));

    //spawn producer
    println!("starting producer");
    let mut prod_done = done.clone();
    let producer = Thread::start(
        proc(){ *( prod_done.lock()) = true;});

    //spawn consumer
    println!("starting consumer");
    let consumer = Thread::start(
        proc(){
            while ! *( done.lock()) {}
        });

    //finish up
    println!("joining threads");
    producer.join();
    consumer.join();
    println!("threads joined");
}

Thanks to some help from people on reddit, I managed to get around it with AtomicBool, but this error still stands.

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