once.qbk 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. [/
  2. (C) Copyright 2007-8 Anthony Williams.
  3. Distributed under the Boost Software License, Version 1.0.
  4. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt).
  6. ]
  7. [section:once One-time Initialization]
  8. #include <boost/thread/once.hpp>
  9. namespace boost
  10. {
  11. struct once_flag;
  12. template<typename Function, class ...ArgTypes>
  13. inline void call_once(once_flag& flag, Function&& f, ArgTypes&&... args);
  14. #if defined BOOST_THREAD_PROVIDES_DEPRECATED_FEATURES_SINCE_V3_0_0
  15. void call_once(void (*func)(),once_flag& flag);
  16. #endif
  17. }
  18. [warning the variadic prototype is provided only on C++11 compilers supporting variadic templates, otherwise the interface is limited up to 3 parameters.]
  19. [warning the move semantics is ensured only on C++11 compilers supporting SFINAE expression, decltype N3276 and auto. Waiting for a boost::bind that is move aware.]
  20. `boost::call_once` provides a mechanism for ensuring that an initialization routine is run exactly once without data races or deadlocks.
  21. [section:once_flag Typedef `once_flag`]
  22. #ifdef BOOST_THREAD_PROVIDES_ONCE_CXX11
  23. struct once_flag
  24. {
  25. constexpr once_flag() noexcept;
  26. once_flag(const once_flag&) = delete;
  27. once_flag& operator=(const once_flag&) = delete;
  28. };
  29. #else
  30. typedef platform-specific-type once_flag;
  31. #define BOOST_ONCE_INIT platform-specific-initializer
  32. #endif
  33. Objects of type `boost::once_flag` shall be initialized with `BOOST_ONCE_INIT` if BOOST_THREAD_PROVIDES_ONCE_CXX11 is not defined
  34. boost::once_flag f=BOOST_ONCE_INIT;
  35. [endsect]
  36. [section:call_once Non-member function `call_once`]
  37. template<typename Function, class ...ArgTypes>
  38. inline void call_once(once_flag& flag, Function&& f, ArgTypes&&... args);
  39. [variablelist
  40. [[Requires:] [`Function` and each or the `ArgTypes` are `MoveConstructible` and `invoke(decay_copy(boost::forward<Function>(f)), decay_copy(boost::forward<ArgTypes>(args))...)` shall be well formed. ]]
  41. [[Effects:] [Calls to `call_once` on the same `once_flag` object are serialized. If there has been no prior effective `call_once` on
  42. the same `once_flag` object, the argument `func` is called as-if by invoking `invoke(decay_copy(boost::forward<Function>(f)), decay_copy(boost::forward<ArgTypes>(args))...)`, and the invocation of
  43. `call_once` is effective if and only if `invoke(decay_copy(boost::forward<Function>(f)), decay_copy(boost::forward<ArgTypes>(args))...)` returns without exception. If an exception is thrown, the exception is propagated to the caller. If there has been a prior effective `call_once` on the same `once_flag` object, the `call_once` returns
  44. without invoking `func`. ]]
  45. [[Synchronization:] [The completion of an effective `call_once` invocation on a `once_flag` object, synchronizes with
  46. all subsequent `call_once` invocations on the same `once_flag` object. ]]
  47. [[Throws:] [`thread_resource_error` when the effects cannot be achieved or any exception propagated from `func`.]]
  48. [[Note:] [The function passed to `call_once` must not also call
  49. `call_once` passing the same `once_flag` object. This may cause
  50. deadlock, or invoking the passed function a second time. The
  51. alternative is to allow the second call to return immediately, but
  52. that assumes the code knows it has been called recursively, and can
  53. proceed even though the call to `call_once` didn't actually call the
  54. function, in which case it could also avoid calling `call_once`
  55. recursively.]]
  56. [[Note:] [On some compilers this function has some restrictions, e.g. if variadic templates are not supported the number of arguments is limited to 3; .]]
  57. ]
  58. void call_once(void (*func)(),once_flag& flag);
  59. This second overload is provided for backwards compatibility and is deprecated. The effects of `call_once(func,flag)` shall be the same as those of
  60. `call_once(flag,func)`.
  61. [endsect]
  62. [endsect]