aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/deps/rpclib/include/rpc/detail/thread_group.h
blob: 8905a71501bbeaf4f3ffce36c6ff8c0d378ac1e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#pragma once

#ifndef THREAD_GROUP_H_MQSLWGKD
#define THREAD_GROUP_H_MQSLWGKD

#include <thread>
#include <vector>

namespace rpc {
namespace detail {

class thread_group {
public:
    thread_group() {}
    thread_group(thread_group const &) = delete;

    void create_threads(std::size_t thread_count, std::function<void()> func) {
        for (std::size_t i = 0; i < thread_count; ++i) {
            threads_.push_back(std::thread(func));
        }
    }

    void join_all() {
        for (auto &t : threads_) {
            if (t.joinable()) {
                t.join();
            }
        }
    }

    ~thread_group() { join_all(); }

private:
    std::vector<std::thread> threads_;
};

} /* detail */
} /* rpc  */

#endif /* end of include guard: THREAD_GROUP_H_MQSLWGKD */