// Copyright 2019 The MediaPipe Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #ifndef MEDIAPIPE_DEPS_THREADPOOL_H_ #define MEDIAPIPE_DEPS_THREADPOOL_H_ #include <deque> #include <functional> #include <string> #include <vector> #include "absl/synchronization/mutex.h" #include "mediapipe/framework/deps/thread_options.h" namespace mediapipe { // A thread pool consists of a set of threads that sit around waiting // for callbacks to appear on a queue. When that happens, one of the // threads pulls a callback off the queue and runs it. // // The thread pool is shut down when the pool is destroyed. // // Sample usage: // // { // ThreadPool pool("testpool", num_workers); // pool.StartWorkers(); // for (int i = 0; i < N; ++i) { // pool.Schedule([i]() { DoWork(i); }); // } // } // class ThreadPool { … }; namespace internal { // Creates name for thread in a thread pool based on provided prefix and // thread id. Length of the resulting name is guaranteed to be less or equal // to 15. Name or thread id can be truncated to achieve that, see truncation // samples below: // name_prefix, 1234 -> name_prefix/123 // name_prefix, 1234567 -> name_prefix/123 // name_prefix_long, 1234 -> name_prefix_lon std::string CreateThreadName(const std::string& prefix, int thread_id); } // namespace internal } // namespace mediapipe #endif // MEDIAPIPE_DEPS_THREADPOOL_H_