Establishes and safely deletes advisory blocking on the open file.
``` extern crate cluFlock;
use cluFlock::ToFlock; use std::fs::File; use std::io;
fn main() -> Result<(), io::Error> { let filelock = File::create("/tmp/1")?.waitexclusive_lock()?;
println!("{:?}", file_lock);
drop(file_lock); //<-- unlock fn.
Ok( () )
} ```
``` extern crate cluFlock;
use std::io::Write; use cluFlock::ToFlock; use std::fs::File; use std::io;
fn main() -> Result<(), io::Error> { File::create("/tmp/1")?.waitexclusivelock_fn(|mut file| { write!(file, "Test.") })??;
Ok( () )
} ```
```
extern crate cluFlock;
use cluFlock::ExclusiveFlock; use std::fs::File;
fn main() -> Result<(), std::io::Error> { let file = File::create("/tmp/1").unwrap();
let file_lock = ExclusiveFlock::wait_lock(&file)?;
//lock...
println!("{:?}", file_lock);
// let file move!
drop(file_lock);
file.sync_all()?;
Ok( () )
} ```
```
extern crate cluFlock;
use cluFlock::ExclusiveFlock; use std::fs::File; use std::time::Duration; use std::io::ErrorKind;
fn main() { let file: File = match File::create("/tmp/ulin.lock") { Ok(a) => a, Err(e) => panic!("Panic, err create file {:?}", e), };
println!("Try_Exclusive_Lock, {:?}", file);
let lock = match ExclusiveFlock::try_lock(&file) {
//Success, we blocked the file.
Ok(lock) => {
println!("OK, File {:?} successfully locked.", file);
lock
},
//File already locked.
Err(ref e) if e.kind() == ErrorKind::WouldBlock => {
println!("ALREADY LOCKED: File {:?}.", file);
println!("!Exclusive_Lock, {:?}", file);
//Lock the current thread to such an extent until your file is unlocked.
//&file.wait_exclusive_lock().unwrap()
ExclusiveFlock::wait_lock(&file).unwrap()
},
Err(e) => panic!("Panic, err lock file {:?}", e)
};
println!("Sleep, 5s");
::std::thread::sleep(Duration::from_secs(5));
println!("Unlock, {:?}", file);
drop(lock);
} ```
Copyright 2018 #UlinProject Денис Котляров
Licensed under the Apache License, Version 2.0