Blender V5.0
semaphore.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#include "util/thread.h"
8
10
11/* Counting Semaphore
12 *
13 * To restrict concurrent access to a resource to a specified number
14 * of threads. Similar to std::counting_semaphore from C++20. */
15
17 public:
18 explicit thread_counting_semaphore(const int count) : count(count) {}
19
21
22 void acquire()
23 {
25 while (count == 0) {
26 condition.wait(lock);
27 }
28 count--;
29 }
30
31 void release()
32 {
34 count++;
35 condition.notify_one();
36 }
37
38 protected:
41 int count;
42};
43
volatile int lock
thread_counting_semaphore(const thread_counting_semaphore &)=delete
thread_condition_variable condition
Definition semaphore.h:40
thread_counting_semaphore(const int count)
Definition semaphore.h:18
#define CCL_NAMESPACE_END
std::mutex thread_mutex
Definition thread.h:27
std::condition_variable thread_condition_variable
Definition thread.h:29
std::unique_lock< std::mutex > thread_scoped_lock
Definition thread.h:28