How to “unlock” an RwLock?

落花浮王杯 提交于 2020-06-13 04:50:08

问题


I'm trying to solve the thread-ring problem. In each thread I read the token value

  • if it is not mine, check if it's the end of the program

    • if it is then finish the thread
    • otherwise, read again and repeat
  • if it is mine (i.e. has my id) then acquire the write lock, increase the value of the token, check if it's the end then tell main thread that I finished it and finish the current thread loop

  • If it not over, then release the write lock, and start to read again

There is no unlock. Is there any unlock like the one I need in here?

It seems that I should release the read lock as well, because the write lock won't happen if there is someone reading the data. Is it necessary?

fn main() {
    use std::sync::{Arc, RwLock};
    use std::thread;
    use std::sync::mpsc::channel;

    const N: usize = 5; //503;
    const STOP_POINT: usize = 100;

    let n = Arc::new(RwLock::new(1));

    let (sender, reciever) = channel();

    for i in 1..N {
        let (n_c, channel) = (n.clone(), sender.clone());
        // println!("Thread n.{} beeing created!", i);

        let a = thread::Builder::new()
            .name(i.to_string())
            .spawn(move || -> () {
                loop {
                    let mut read_only = n_c.read().unwrap();
                    let say_my_name = (*thread::current().name().unwrap()).to_string();

                    // println!("Thread {} says: gonna try!", say_my_name);
                    while (*read_only % N) != i {
                        if *read_only == 0 {
                            break;
                        }
                        // println!("Thread {} says: aint mine!", say_my_name);
                        read_only = n_c.read().unwrap();
                    } // WAIT

                    println!("Thread {} says: my turn!", say_my_name);
                    let mut ref_to_num = n_c.write().unwrap();
                    *ref_to_num += 1;

                    if *ref_to_num == STOP_POINT {
                        channel.send(say_my_name).unwrap();
                        break;
                    }
                }
                ()
            });
        assert_eq!(a.is_ok(), true);
        // thread::spawn();
        // println!("Thread n.{} created!", i);
    }

    println!("{}", reciever.recv().unwrap());
}

回答1:


To release a lock, you let it fall out of scope or explicitly invoke its destructor by calling drop.

Here's how your program could be written using drop in two places:

fn main() {
    use std::sync::{Arc, RwLock};
    use std::sync::mpsc::channel;
    use std::thread;
    use std::time::Duration;

    const N: usize = 503;
    const STOP_POINT: usize = 100;

    let n = Arc::new(RwLock::new(1));

    let (sender, receiver) = channel();

    for i in 1..N {
        let (n_c, channel) = (n.clone(), sender.clone());
        // println!("Thread n.{} beeing created!", i);

        thread::Builder::new()
            .name(i.to_string())
            .spawn(move || {
                loop {
                    let mut read_only = n_c.read().unwrap();
                    let say_my_name = (*thread::current().name().unwrap()).to_string();

                    // println!("Thread {} says: gonna try!", say_my_name);
                    while (*read_only % N) != i {
                        if *read_only == 0 {
                            break;
                        }

                        drop(read_only); // release the lock before sleeping
                        // println!("Thread {} says: aint mine!", say_my_name);
                        thread::sleep(Duration::from_millis(1));
                        read_only = n_c.read().unwrap();
                    }

                    println!("Thread {} says: my turn!", say_my_name);
                    drop(read_only); // release the read lock before taking a write lock
                    let mut ref_to_num = n_c.write().unwrap();
                    *ref_to_num += 1;

                    if *ref_to_num == STOP_POINT {
                        channel.send(say_my_name).unwrap();
                        break;
                    }
                }
            })
            .expect("failed to spawn a thread");
        // println!("Thread n.{} created!", i);
    }

    println!("{}", receiver.recv().unwrap());
}

Note that if we don't reassign read_lock in the while loop, the compiler will give an error because read_lock doesn't hold a valid value after we call drop(read_lock). Rust is fine with local variables that are temporarily uninitialized, but of course we need to reinitialize them before we can use them again.

Here's how the thread's main loop could be written to use a scope to replace one of the drops:

loop {
    let say_my_name = (*thread::current().name().unwrap()).to_string();
    {
        let mut read_only = n_c.read().unwrap();

        // println!("Thread {} says: gonna try!", say_my_name);
        while (*read_only % N) != i {
            if *read_only == 0 {
                break;
            }

            drop(read_only);
            thread::sleep(Duration::from_millis(1));
            // println!("Thread {} says: aint mine!", say_my_name);
            read_only = n_c.read().unwrap();
        }

        println!("Thread {} says: my turn!", say_my_name);
    } // read_only is dropped here

    let mut ref_to_num = n_c.write().unwrap();
    *ref_to_num += 1;

    if *ref_to_num == STOP_POINT {
        channel.send(say_my_name).unwrap();
        break;
    }
}


来源:https://stackoverflow.com/questions/47916718/how-to-unlock-an-rwlock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!