pub fn try_acquire(&self) -> Option<SemaphorePermit> {
        let backoff = Backoff::new();
        let mut permits = self.permits.load(Ordering::Relaxed);
        loop {
            if permits == 0 {
                return None;
            }
            match self.permits.compare_exchange_weak(
                permits,
                permits - 1,
                Ordering::Acquire,
                Ordering::Relaxed,
            ) {
                Ok(_) => return Some(SemaphorePermit::new(self)),
                Err(changed) => permits = changed,
            }
            backoff.spin();
        }
    }
/// Tries to acquire a permit from the semaphore if there is one available.
///
/// Returns `None` immediately if there are no idle resources available in the pool.
///
/// # Examples
///
/// ```
/// # use qp::sync::Semaphore;
/// # #[tokio::main]
/// # async fn main() {
/// let binary_semaphore = Semaphore::new(1);
/// let permit1 = binary_semaphore.try_acquire();
/// assert!(permit1.is_some());
/// let permit2 = binary_semaphore.try_acquire();
/// assert!(permit2.is_none());
/// drop(permit1);
/// let permit3 = binary_semaphore.try_acquire();
/// assert!(permit3.is_some());
/// # }
Tries to acquire a permit from the semaphore if there is one available.
Returns `None` immediately if there are no idle resources available in the pool.
Tries to acquire a permit from the semaphore if there is one available . Returns ` None ` immediately if there are no idle resources available in the pool .
pub fn try_acquire(&self) -> Option<SemaphorePermit> {
        let backoff = Backoff::new();
        let mut permits = self.permits.load(Ordering::Relaxed);
        loop {
            if permits == 0 {
                return None;
            }
            match self.permits.compare_exchange_weak(
                permits,
                permits - 1,
                Ordering::Acquire,
                Ordering::Relaxed,
            ) {
                Ok(_) => return Some(SemaphorePermit::new(self)),
                Err(changed) => permits = changed,
            }
            backoff.spin();
        }
    }
pub fn try_acquire ( & self ) -> Option < SemaphorePermit > { let backoff = Backoff :: new ( ) ; let mut permits = self . permits . load ( Ordering :: Relaxed ) ; loop { if permits == 0 { return None ; } match self . permits . compare_exchange_weak ( permits , permits - 1 , Ordering :: Acquire , Ordering :: Relaxed , ) { Ok ( _ ) => return Some ( SemaphorePermit :: new ( self ) ) , Err ( changed ) => permits = changed , } backoff . spin ( ) ; } }
Tries to acquire a permit from the semaphore if there is one available.
Tries to acquire a permit from the semaphore if there is one available .
&self
examples
None
use qp::sync::semaphore;
None
#[tokio::main]
None
async fn main() {
let binary_semaphore = Semaphore::new(1);
let permit1 = binary_semaphore.try_acquire();
assert!(permit1.is_some());
let permit2 = binary_semaphore.try_acquire();
assert!(permit2.is_none());
drop(permit1);
let permit3 = binary_semaphore.try_acquire();
assert!(permit3.is_some()).
let binary_semaphore = Semaphore :: new ( 1 ) ; let permit1 = binary_semaphore . try_acquire () ; assert! ( permit1 . is_some () ) ; let permit2 = binary_semaphore . try_acquire () ; assert! ( permit2 . is_none () ) ; drop ( permit1 ) ; let permit3 = binary_semaphore . try_acquire () ; assert! ( permit3 . is_some () ) .
}
None