# Struct std::sync::Once 1.0.0 [−] [src]

    pub struct Once {
        // some fields omitted
    }

A synchronization primitive which can be used to run a one-time global initialization. Useful for one-time initialization for FFI or related functionality. This type can only be constructed with the `ONCE_INIT` value.

# Examples

fn main() { use std::sync::{Once, ONCE_INIT}; static START: Once = ONCE_INIT; START.call_once(|| { // run initialization here }); }

    use std::sync::{Once, ONCE_INIT};

    static START: Once = ONCE_INIT;

    START.call_once(|| {
        // run initialization here
    });

## Methods

### `impl Once`
[src]

#### `const fn new() -> Once`
1.2.0

Creates a new `Once` value.

#### `fn call_once<F>(&'static self, f: F) where F: FnOnce()`

Performs an initialization routine once and only once. The given closure will be executed if this is the first time `call_once` has been called, and otherwise the routine will not be invoked.

This method will block the calling thread if another initialization routine is currently running.

When this function returns, it is guaranteed that some initialization has run and completed (it may not be the closure specified). It is also guaranteed that any memory writes performed by the executed closure can be reliably observed by other threads at this point (there is a happens-before relation between the closure and code executing after the return).

# Examples

fn main() { use std::sync::{Once, ONCE_INIT}; static mut VAL: usize = 0; static INIT: Once = ONCE_INIT; // Accessing a `static mut` is unsafe much of the time, but if we do so // in a synchronized fashion (e.g. write once or read all) then we're // good to go! // // This function will only call `expensive_computation` once, and will // otherwise always return the value returned from the first invocation. fn get_cached_val() -> usize { unsafe { INIT.call_once(|| { VAL = expensive_computation(); }); VAL } } fn expensive_computation() -> usize { // ... 2 } }

    use std::sync::{Once, ONCE_INIT};

    static mut VAL: usize = 0;
    static INIT: Once = ONCE_INIT;

    // Accessing a `static mut` is unsafe much of the time, but if we do so
    // in a synchronized fashion (e.g. write once or read all) then we're
    // good to go!
    //
    // This function will only call `expensive_computation` once, and will
    // otherwise always return the value returned from the first invocation.
    fn get_cached_val() -> usize {
        unsafe {
            INIT.call_once(|| {
                VAL = expensive_computation();
            });
            VAL
        }
    }

    fn expensive_computation() -> usize {
        // ...
    }

# Panics

The closure `f` will only be executed once if this is called concurrently amongst many threads. If that closure panics, however, then it will poison this `Once` instance, causing all future invocations of `call_once` to also panic.

This is similar to poisoning with mutexes.

#### `fn call_once_force<F>(&'static self, f: F) where F: FnOnce(&OnceState)`

Unstable (`once_poison` #33577)

Performs the same function as `call_once` except ignores poisoning.

If this `Once` has been poisoned (some initialization panicked) then this function will continue to attempt to call initialization functions until one of them doesn't panic.

The closure `f` is yielded a structure which can be used to query the state of this `Once` (whether initialization has previously panicked or not).

## Trait Implementations

### `impl Sync for Once`
[src]

### `impl Send for Once`
[src]

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result
+
  Collapse/expand all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)