- Fejhallgató erősítő és DAC topik
- Házimozi belépő szinten
- Kormányok / autós szimulátorok topikja
- Milyen billentyűzetet vegyek?
- AMD K6-III, és minden ami RETRO - Oldschool tuning
- Papíron már készül a PCI Express 8.0
- Zászlóshajót avatott a Lexar SSD-kben utazó divíziója
- OLED TV topic
- NVIDIA GeForce RTX 5070 / 5070 Ti (GB205 / 203)
- HiFi műszaki szemmel - sztereó hangrendszerek
Új hozzászólás Aktív témák
-
cog777
senior tag
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!
- Új telefont és tabletet mutatott be a Telekom
- Kerékpárosok, bringások ide!
- Motorola Edge 50 Neo - az egyensúly gyengesége
- Milyen légkondit a lakásba?
- Magisk
- Fejhallgató erősítő és DAC topik
- Audi, Cupra, Seat, Skoda, Volkswagen topik
- Autóápolás, karbantartás, fényezés
- Házimozi belépő szinten
- Kormányok / autós szimulátorok topikja
- További aktív témák...
- Dell Latitude 5320 -60% "Kis Gamer" Üzleti Profi Ultrabook 13,3" i5-1145G7 8/256 FHD IRIS Xe
- Apple IPad pro 12.9 4th gen 256GB wifi+sim 97%-os Gyári akku
- PlayStation 5 (PS5.) SLIM 1TB. SSD. Digital Edition & Sony PlayStation VR2. Virtuális szemüveg.
- Dell Precision 3580 i7-1370P//32GB DDR5 RAM// 1TB SSD/ RTX A500
- ÁRCSÖKKENTÉS Menő retró konfig: Q9550, Gigabyte P43, 4GB RAM, ASUS GT730
- Bomba ár! HP ProBook 650 G4 - i5-8GEN I 8GB I 256GB SSD I 15,6" FHD I Cam I W11 I Garancia!
- GYÖNYÖRŰ iPhone 13 Pro Max 1TB Gold -1 ÉV GARANCIA - Kártyafüggetlen, MS3051
- BESZÁMÍTÁS! 3TB Western Digital WD RED SATA HDD meghajtó garanciával hibátlan működéssel
- ÁRGARANCIA!Épített KomPhone i5 14600KF 32/64GB RAM RTX 5070 12GB GAMER PC termékbeszámítással
- AKCIÓ! Gigabyte Z370M i5 9400F 16GB DDR4 512GB SSD RX 5700XT 8GB ZALMAN S2 TG Corsair S650W
Állásajánlatok
Cég: FOTC
Város: Budapest