You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

barrier.h 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // -----------------------------------------------------------------------------
  16. // barrier.h
  17. // -----------------------------------------------------------------------------
  18. #ifndef ABSL_SYNCHRONIZATION_BARRIER_H_
  19. #define ABSL_SYNCHRONIZATION_BARRIER_H_
  20. #include "absl/base/thread_annotations.h"
  21. #include "absl/synchronization/mutex.h"
  22. namespace absl
  23. {
  24. ABSL_NAMESPACE_BEGIN
  25. // Barrier
  26. //
  27. // This class creates a barrier which blocks threads until a prespecified
  28. // threshold of threads (`num_threads`) utilizes the barrier. A thread utilizes
  29. // the `Barrier` by calling `Block()` on the barrier, which will block that
  30. // thread; no call to `Block()` will return until `num_threads` threads have
  31. // called it.
  32. //
  33. // Exactly one call to `Block()` will return `true`, which is then responsible
  34. // for destroying the barrier; because stack allocation will cause the barrier
  35. // to be deleted when it is out of scope, barriers should not be stack
  36. // allocated.
  37. //
  38. // Example:
  39. //
  40. // // Main thread creates a `Barrier`:
  41. // barrier = new Barrier(num_threads);
  42. //
  43. // // Each participating thread could then call:
  44. // if (barrier->Block()) delete barrier; // Exactly one call to `Block()`
  45. // // returns `true`; that call
  46. // // deletes the barrier.
  47. class Barrier
  48. {
  49. public:
  50. // `num_threads` is the number of threads that will participate in the barrier
  51. explicit Barrier(int num_threads) :
  52. num_to_block_(num_threads),
  53. num_to_exit_(num_threads)
  54. {
  55. }
  56. Barrier(const Barrier&) = delete;
  57. Barrier& operator=(const Barrier&) = delete;
  58. // Barrier::Block()
  59. //
  60. // Blocks the current thread, and returns only when the `num_threads`
  61. // threshold of threads utilizing this barrier has been reached. `Block()`
  62. // returns `true` for precisely one caller, which may then destroy the
  63. // barrier.
  64. //
  65. // Memory ordering: For any threads X and Y, any action taken by X
  66. // before X calls `Block()` will be visible to Y after Y returns from
  67. // `Block()`.
  68. bool Block();
  69. private:
  70. Mutex lock_;
  71. int num_to_block_ ABSL_GUARDED_BY(lock_);
  72. int num_to_exit_ ABSL_GUARDED_BY(lock_);
  73. };
  74. ABSL_NAMESPACE_END
  75. } // namespace absl
  76. #endif // ABSL_SYNCHRONIZATION_BARRIER_H_