Új hozzászólás Aktív témák
-
cog777
őstag
Tobb megoldast kaptam, itt lehet elolvasni.
Lenyeg: van lehetoseg trukkozni, de az tenyleg trukkozes, inkabb template specializaciot valasztanek.Ez a megoldas tetszett jobban:
#include <fmt/format.h>
#include <array>
#include <cstddef>
#include <mutex>
enum class thread_safety_mode {
safe,
unsafe,
};
template <typename T, std::size_t Size, thread_safety_mode Mode>
class circular_buffer;
template <typename T, std::size_t Size>
class circular_buffer<T, Size, thread_safety_mode::unsafe> {
public:
// Push an item to the tail
bool push(const T& item) {
if (is_full()) return false;
buffer_[head_] = item;
head_ = (head_ + 1) % buffer_.size();
return true;
}
// Pop an item from the head
bool pop(T& item) {
if (is_empty()) return false;
item = buffer_[tail_];
tail_ = (tail_ + 1) % buffer_.size();
return true;
}
// Check if the buffer is full
bool is_full() const { return (head_ + 1) % buffer_.size() == tail_; }
// Check if the buffer is empty
bool is_empty() const { return head_ == tail_; }
private:
std::array<T, Size> buffer_;
std::size_t head_{0};
std::size_t tail_{0};
};
template <typename T, std::size_t Size>
class circular_buffer<T, Size, thread_safety_mode::safe> {
public:
bool push(const T& item) {
std::lock_guard<std::mutex> lk(m);
fmt::println("locked for push()");
return unsafe_buffer.push(item);
}
// Pop an item from the head
bool pop(T& item) {
std::lock_guard<std::mutex> lk(m);
fmt::println("locked for pop()");
return unsafe_buffer.pop(item);
}
// Check if the buffer is full
bool is_full() const {
std::lock_guard<std::mutex> lk(m);
fmt::println("locked for is_full()");
return unsafe_buffer.is_full();
}
// Check if the buffer is empty
bool is_empty() const {
std::lock_guard<std::mutex> lk(m);
fmt::println("locked for is_empty()");
return unsafe_buffer.is_empty();
}
private:
circular_buffer<T, Size, thread_safety_mode::unsafe> unsafe_buffer{};
mutable std::mutex m{};
};
auto main() -> int {
//
circular_buffer<int, 42, thread_safety_mode::safe> cb{};
cb.push(43);
cb.push(44);
cb.push(45);
int val;
cb.pop(val);
fmt::println("val: {}", val);
cb.pop(val);
fmt::println("val: {}", val);
cb.pop(val);
fmt::println("val: {}", val);
}
Új hozzászólás Aktív témák
● ha kódot szúrsz be, használd a PROGRAMKÓD formázási funkciót!
- Launch trailert kapott a Honkai: Star Rail
- Olcsó vs. drága egér: melyiknél érzed meg igazán a különbséget?
- Temu
- Steam topic
- Gumi és felni topik
- Fejhallgatós találkozó
- Fejhallgató erősítő és DAC topik
- Assetto Corsa Rally
- Kodi és kiegészítői magyar nyelvű online tartalmakhoz (Linux, Windows)
- KAÜ/Ügyfélkapu – már elérhető a kétfaktoros hitelesítés
- További aktív témák...
- Xiaomi Redmi 13 128GB, Kártyafüggetlen, 1 Év Garanciával
- Geforce GTX 1050, 1050 Ti, 1060, 1650, 1660 - GT 1030 - Low profile is (LP)
- Samsung Galaxy S24 Ultra 120 Hz Dynamic AMOLED 2X, beépített S Pen, Galaxy AI 12/256 GB
- ÁRGARANCIA!Épített KomPhone i5 14400F 32/64GB RAM RX 9060 XT 16GB GAMER PC termékbeszámítással
- ÁRGARANCIA!Épített KomPhone Ryzen 5 7500F 32/64GB RAM RX 7800 XT 16GB GAMER PC termékbeszámítással
Állásajánlatok
Cég: Laptopműhely Bt.
Város: Budapest
Cég: ATW Internet Kft.
Város: Budapest

