barrier.h
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 //
00015 // -----------------------------------------------------------------------------
00016 // barrier.h
00017 // -----------------------------------------------------------------------------
00018 
00019 #ifndef ABSL_SYNCHRONIZATION_BARRIER_H_
00020 #define ABSL_SYNCHRONIZATION_BARRIER_H_
00021 
00022 #include "absl/base/thread_annotations.h"
00023 #include "absl/synchronization/mutex.h"
00024 
00025 namespace absl {
00026 
00027 // Barrier
00028 //
00029 // This class creates a barrier which blocks threads until a prespecified
00030 // threshold of threads (`num_threads`) utilizes the barrier. A thread utilizes
00031 // the `Barrier` by calling `Block()` on the barrier, which will block that
00032 // thread; no call to `Block()` will return until `num_threads` threads have
00033 // called it.
00034 //
00035 // Exactly one call to `Block()` will return `true`, which is then responsible
00036 // for destroying the barrier; because stack allocation will cause the barrier
00037 // to be deleted when it is out of scope, barriers should not be stack
00038 // allocated.
00039 //
00040 // Example:
00041 //
00042 //   // Main thread creates a `Barrier`:
00043 //   barrier = new Barrier(num_threads);
00044 //
00045 //   // Each participating thread could then call:
00046 //   if (barrier->Block()) delete barrier;  // Exactly one call to `Block()`
00047 //                                          // returns `true`; that call
00048 //                                          // deletes the barrier.
00049 class Barrier {
00050  public:
00051   // `num_threads` is the number of threads that will participate in the barrier
00052   explicit Barrier(int num_threads)
00053       : num_to_block_(num_threads), num_to_exit_(num_threads) {}
00054 
00055   Barrier(const Barrier&) = delete;
00056   Barrier& operator=(const Barrier&) = delete;
00057 
00058   // Barrier::Block()
00059   //
00060   // Blocks the current thread, and returns only when the `num_threads`
00061   // threshold of threads utilizing this barrier has been reached. `Block()`
00062   // returns `true` for precisely one caller, which may then destroy the
00063   // barrier.
00064   //
00065   // Memory ordering: For any threads X and Y, any action taken by X
00066   // before X calls `Block()` will be visible to Y after Y returns from
00067   // `Block()`.
00068   bool Block();
00069 
00070  private:
00071   Mutex lock_;
00072   int num_to_block_ GUARDED_BY(lock_);
00073   int num_to_exit_ GUARDED_BY(lock_);
00074 };
00075 
00076 }  // namespace absl
00077 #endif  // ABSL_SYNCHRONIZATION_BARRIER_H_


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:14