cpp-stat-bench 0.24.0
Benchmark library with statistics for C++.
Loading...
Searching...
No Matches
pthread_sync_barrier.h
Go to the documentation of this file.
1/*
2 * Copyright 2023 MusicScience37 (Kenta Kabashima)
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 */
20#pragma once
21
22#include <cstddef>
23
24#include <fmt/format.h>
25#include <pthread.h>
26
29
30namespace stat_bench {
31namespace util {
32
36class PthreadSyncBarrier final : public ISyncBarrier {
37public:
43 explicit PthreadSyncBarrier(std::size_t num_waiting_threads) {
44 if (num_waiting_threads < 2U) {
46 "Invalid number of threads to wait in PthreadSyncBarrier.");
47 }
48 const int error_code = pthread_barrier_init(
49 &barrier_, nullptr, static_cast<unsigned int>(num_waiting_threads));
50 if (error_code != 0) {
51 throw StatBenchException(fmt::format(
52 "Failed to initialize PthreadSyncBarrier (error code: {}).",
53 error_code));
54 }
55 }
56
60 void wait() override { pthread_barrier_wait(&barrier_); }
61
64 auto operator=(const PthreadSyncBarrier&) = delete;
65 auto operator=(PthreadSyncBarrier&&) = delete;
66
68 ~PthreadSyncBarrier() override { pthread_barrier_destroy(&barrier_); }
69
70private:
72 pthread_barrier_t barrier_{};
73};
74
75} // namespace util
76} // namespace stat_bench
Class of exceptions in this library.
Class of barriers to synchronize threads using pthread library.
void wait() override
Wait for other threads.
PthreadSyncBarrier(std::size_t num_waiting_threads)
Constructor.
Namespace of utility functions and classes.
Namespace of stat_bench source codes.
Definition of StatBenchException class.
Definition of SyncBarrier class.