Hirdetés
- NVIDIA GeForce RTX 5080 / 5090 (GB203 / 202)
- CES 2026: jön az AMD CES előadása és az NVIDIA GeForce ON
- Milyen videókártyát?
- A legrosszabb CPU-k – az ExtremeTech szerint
- OLED TV topic
- Hogy is néznek ki a gépeink?
- AMD K6-III, és minden ami RETRO - Oldschool tuning
- eGPU tapasztalatok
- HiFi műszaki szemmel - sztereó hangrendszerek
- Azonnali alaplapos kérdések órája
Új hozzászólás Aktív témák
-
cog777
őstag
Le tudna valaki csekkolni ezt a kodot? Tok mas teruleten dolgoztam mostanaban es radobbentem, hogy bizonytalan vagyok most ebben az alap kerdesben. Consumer-producer, thread safe circular buffer-rel + recursize mutex.
Lefordul, mukodik crash nelkul, de kivancsi vagyok hogy elszabtam-e valamit... koszi elore is.

Circular buffer kodja:
circular_buffer.h#pragma once
#include <array>
#include <stddef.h>
#include <mutex>
template <typename T, size_t S>
class circular_buffer
{
public:
explicit circular_buffer() {}
// Push an item to the tail
bool push(const T &item)
{
std::lock_guard<std::recursive_mutex> lk(m);
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)
{
std::lock_guard<std::recursive_mutex> lk(m);
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
{
std::lock_guard<std::recursive_mutex> lk(m);
return (head_ + 1) % buffer_.size() == tail_;
}
// Check if the buffer is empty
bool is_empty() const
{
std::lock_guard<std::recursive_mutex> lk(m);
return head_ == tail_;
}
private:
std::array<T, S> buffer_;
size_t head_{0};
size_t tail_{0};
mutable std::recursive_mutex m;
};main.cpp
#include "circular_buffer.h"
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
circular_buffer<int, 5> buffer;
std::mutex mtx; // Mutex for protecting shared data
std::condition_variable empty, full; // Condition variables
void producer(int loops)
{
for (int i = 0; i < loops; ++i)
{
std::unique_lock<std::mutex> lock(mtx);
while (buffer.is_full())
{
empty.wait(lock); // Wait if buffer is full
}
buffer.push(i);
full.notify_one(); // Signal that buffer is not empty
std::cout << "Produced: " << i << std::endl;
}
}
void consumer(int loops)
{
for (int i = 0; i < loops; ++i)
{
std::unique_lock<std::mutex> lock(mtx);
while (buffer.is_empty())
{
full.wait(lock); // Wait if buffer is empty
}
int tmp;
buffer.pop(tmp);
empty.notify_one(); // Signal that buffer is not full
std::cout << "Consumed: " << tmp << std::endl;
}
}
int main()
{
int loops = 10;
std::thread prodThread(producer, loops);
std::thread consThread(consumer, loops);
prodThread.join();
consThread.join();
return 0;
}
Ú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!
- Visszatérnek a Samsung tervezte CPU-magok és GPU az Exynos 2800-ban?
- Rugalmas OLED panelre válthat a Samsung Galaxy A57
- czundermák: Shikoku Henro #0: Mégis mi ez?
- One otthoni szolgáltatások (TV, internet, telefon)
- Milyen okostelefont vegyek?
- Samsung Galaxy A54 - türelemjáték
- NVIDIA GeForce RTX 5080 / 5090 (GB203 / 202)
- sziku69: Fűzzük össze a szavakat :)
- Luck Dragon: Asszociációs játék. :)
- Vicces képek
- További aktív témák...
- Szép! Lenovo Thinkpad T14s G2 Üzleti "Golyóálló" Laptop 14" -50% i5-1135G7 4Mag 16GB/512GB FHD IPS
- Bomba ár! Lenovo ThinkPad Yoga 370 - i5-G7 I 8GB I 256SSD I 13,3" FHD Touch I W11 I Cam I Gari!
- Bomba ár! Lenovo ThinkPad Yoga 260 - i5-G6 I 8GB I 256SSD I 12,5" Touch I W11 I Cam I Gari!
- HP EliteBook 850 G8 Fémházas Tartós Laptop 15,6" -65% i7-1165G7 16/512 Iris Xe FHD
- Bomba ár! Lenovo ThinkPad X390: i5-G8 I 16GB I 256-1TSSD I 13,3" FHD Touch I HDMI I Cam I W11 I Gar
- Gamer egerek és billentyűzetek kitűnő árakon!
- PC konfig /Ryzen 7 9800X3D, 32GB RAM, 1TB SSD/ akciós áron eladó! BeszámítOK!
- Vállalom Xianomi Okos kamerák, szoftveres javíttását
- ÚJ Lenovo ThinkPad T16 Gen 4 - 16" WUXGA - Ultra 7 255U - 32GB - 1TB SSD - Win11 - 3 év garancia
- Xiaomi 14 512GB,Újszerű,Dobozaval,12 hónap garanciával
Állásajánlatok
Cég: Laptopszaki Kft.
Város: Budapest
Cég: PCMENTOR SZERVIZ KFT.
Város: Budapest



