proxygen
ThreadedRepeatingFunctionRunner.h
Go to the documentation of this file.
1 /*
2  * Copyright 2014-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <folly/Function.h>
20 #include <condition_variable>
21 #include <thread>
22 #include <vector>
23 
24 namespace folly {
25 
105  public:
106  // Returns how long to wait before the next repetition. Must not throw.
108 
111 
118  void stop();
119 
133  void add(
135  RepeatingFn f,
136  std::chrono::milliseconds initialSleep = std::chrono::milliseconds(0));
137 
138  size_t size() const {
139  return threads_.size();
140  }
141 
142  private:
143  // Returns true if this is the first stop().
144  bool stopImpl();
145 
146  // Sleep for a duration, or until stop() is called.
147  bool waitFor(std::chrono::milliseconds duration) noexcept;
148 
149  // Noexcept allows us to get a good backtrace on crashes -- otherwise,
150  // std::terminate would get called **outside** of the thread function.
151  void executeInLoop(
152  RepeatingFn,
153  std::chrono::milliseconds initialSleep) noexcept;
154 
156  bool stopping_{false}; // protected by stopMutex_
157  std::condition_variable stopCv_;
158 
159  std::vector<std::thread> threads_;
160 };
161 
162 } // namespace folly
auto f
bool waitFor(std::chrono::milliseconds duration) noexcept
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
requires E e noexcept(noexcept(s.error(std::move(e))))
const char * name
Definition: http_parser.c:437
void executeInLoop(RepeatingFn, std::chrono::milliseconds initialSleep) noexcept
std::mutex mutex
void add(std::string name, RepeatingFn f, std::chrono::milliseconds initialSleep=std::chrono::milliseconds(0))
const char * string
Definition: Conv.cpp:212