1个不稳定版本

0.1.0 2020年8月12日

#707 in 并发

MIT/Apache

7KB
120

Dekker

此crate提供了德克勒算法的实现。德克勒算法是已知第一个正确的并发编程互斥问题的解决方案。

示例

use dekker::Dekker;
use std::thread;

fn main() {
    // Create two process handles.
    let (mut p1, mut p2) = Dekker::new(0);

    // Create a new thread and move one handle.
    let other = thread::spawn(move || {
        // Increment by one five times.
        for _ in 0..5 {
            println!("Incrementing in secondary thread.");
            *p2.lock() += 1;
        }
    });

    // Increment by one another five times.
    for _ in 0..5 {
        println!("Incrementing in main thread.");
        *p1.lock() += 1;
    }

    // Join the threads.
    other.join().unwrap();

    println!("The counter is now at {}.", *p1.lock());
}

可能的输出如下

Incrementing in main thread.
Incrementing in secondary thread.
Incrementing in secondary thread.
Incrementing in main thread.
Incrementing in secondary thread.
Incrementing in main thread.
Incrementing in main thread.
Incrementing in secondary thread.
Incrementing in secondary thread.
Incrementing in main thread.
The counter is now at 10.

最终计数器将始终为10。

无运行时依赖