Parolin 0.7.9 6796
Console (soon DLLs) to do a tar like job
Loading...
Searching...
No Matches
ThreadPool.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 * All rights reserved.
4 *
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
8 */
9#pragma once
10
11#include "utils/WorkQueue.h"
12
13#include <cstddef>
14#include <functional>
15#include <thread>
16#include <vector>
17
18namespace pzstd {
20class ThreadPool {
21 std::vector<std::thread> threads_;
22
23 WorkQueue<std::function<void()>> tasks_;
24
25 public:
27 explicit ThreadPool(std::size_t numThreads) {
28 threads_.reserve(numThreads);
29 for (std::size_t i = 0; i < numThreads; ++i) {
30 threads_.emplace_back([this] {
31 std::function<void()> task;
32 while (tasks_.pop(task)) {
33 task();
34 }
35 });
36 }
37 }
38
41 tasks_.finish();
42 for (auto& thread : threads_) {
43 thread.join();
44 }
45 }
46
54 void add(std::function<void()> task) {
55 tasks_.push(std::move(task));
56 }
57};
58}
ThreadPool(std::size_t numThreads)
Constructs a thread pool with numThreads threads.
Definition ThreadPool.h:27
void add(std::function< void()> task)
Definition ThreadPool.h:54
~ThreadPool()
Finishes all tasks currently in the queue.
Definition ThreadPool.h:40
Definition ErrorHolder.h:16
lzma_index ** i
Definition index.h:629