std::call_once - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::call_once

From cppreference.com
< cpp‎ | thread

Defined in header `<mutex>` |  |
template< class Callable, class... Args > void call_once( std::once_flag& flag, Callable&& f, Args&&... args ); |  | (since C++11)

Executes the `Callable` object `f` exactly once, even if called from several threads.

Each group of `call_once` invocations that receives the same std::once_flag object will meet the following requirements:

- Exactly one execution of exactly one of the functions (passed as `f` to the invocations in the group) is performed. It is undefined which function will be selected for execution. The selected function runs in the same thread as the `call_once` invocation it was passed to.

- No invocation in the group returns before the abovementioned execution of the selected function is completed successfully, that is, doesn't exit via an exception.

- If the selected function exits via exception, it is propagated to the caller. Another function is then selected and executed.

### [edit] Parameters

flag | - | an object, for which exactly one function gets executed
f | - | `Callable` object to invoke
args... | - | arguments to pass to the function

### [edit] Return value

(none)

### [edit] Exceptions

- std::system_error if any condition prevents calls to `call_once` from executing as specified
- any exception thrown by `f`

### [edit] Example

Run this code

    #include <iostream>
    #include <thread>
    #include <mutex>

    std::once_flag flag;

    void do_once()
    {
        std::call_once(flag, [](){ std::cout << "Called once" << std::endl; });
    }

    int main()
    {
        std::thread t1(do_once);
        std::thread t2(do_once);
        std::thread t3(do_once);
        std::thread t4(do_once);

        t1.join();
        t2.join();
        t3.join();
        t4.join();
    }

Output:

    Called once

### [edit] Example

Run this code

    #include <iostream>
    #include <thread>
    #include <mutex>

    std::once_flag flag;

    inline void may_throw_function(bool do_throw)
    {
      // only one instance of this function can be run simultaneously
      if (do_throw) {
        std::cout << "throw\n"; // this message may be printed from 0 to 3 times
        // if function exits via exception, another function selected
        throw std::exception();
      }

      std::cout << "once\n"; // printed exactly once, it's guaranteed that
          // there are no messages after it
    }

    inline void do_once(bool do_throw)
    {
      try {
        std::call_once(flag, may_throw_function, do_throw);
      }
      catch (...) {
      }
    }

    int main()
    {
        std::thread t1(do_once, true);
        std::thread t2(do_once, true);
        std::thread t3(do_once, false);
        std::thread t4(do_once, true);

        t1.join();
        t2.join();
        t3.join();
        t4.join();
    }

Possible output:

    throw
    throw
    once

### [edit] See also

once_flag (C++11) | helper object to ensure that `call_once` invokes the function only once (class) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/thread/call_once&oldid=70683"