Hirdetés
- eBay-es kütyük kis pénzért
- Luck Dragon: Asszociációs játék. :)
- gban: Ingyen kellene, de tegnapra
- [K2]: AnyDesk átverés
- sziku69: Fűzzük össze a szavakat :)
- VHS digitalizálás
- sziku69: Szólánc.
- Brogyi: CTEK akkumulátor töltő és másolatai
- Lalikiraly: Astra kalandok @ Negyedik rész
- ubyegon2: Airfryer XL XXL forrólevegős sütő gyakorlati tanácsok, ötletek, receptek
Ú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!
- Formula-1
- Milyen videókártyát?
- Android játékok topikja
- AMD GPU-k jövője - amit tudni vélünk
- Konkrét moderációval kapcsolatos kérdések
- Telekom mobilszolgáltatások
- Milyen egeret válasszak?
- One otthoni szolgáltatások (TV, internet, telefon)
- Samsung Galaxy S25 Ultra - titán keret, acélos teljesítmény
- Samsung Galaxy S25 - végre van kicsi!
- További aktív témák...
- Lenovo ideapad 330S laptop - Ryzen5 2500U - 8GB - 250GB SSD
- Land Rover Explore R 4/64GB, Megkímélt, Kártyafüggetlen, Töltővel, 1 Év Garanciával!
- Xiaomi 12T 8/128GB Normál, Kártyafüggetlen, Töltővel, 1 Év Garanciával!
- Xiaomi Redmi Note 13 Pro+ 5G 12/512GB, Normél, Kártyafüggetlen, Töltővel, Dobozzal, 1 Év Garanciával
- Minőségi tápegységek,650-1400W-ig,számlával,garanciával,EVGA,HP,FSP,Cougar,Cooler Master,stb
- Apple iPhone 13 128 GB Fekete 1 év Garancia Beszámítás Házhozszállítás
- HIBÁTLAN iPhone 14 Pro Max 128GB Purple -1 ÉV GARANCIA - Kártyafüggetlen, MS3918, 100% Akkumulátor
- LG 77CS6 - 77" OLED - 4K 120Hz & 1ms - NVIDIA G-Sync - FreeSync Premium - HDMI 2.1 - Szupervékony
- GYÖNYÖRŰ iPhone 11 Pro Max 64GB Midnight Green -1 ÉV GARANCIA - Kártyafüggetlen, MS3253, 100% Akksi
- Bomba ár! Dell Latitude E7240 - i5-4GEN I 8GB I 256SSD I 12,5" HD I HDMI I Cam I W10 I Garancia!
Állásajánlatok
Cég: Laptopműhely Bt.
Város: Budapest
Cég: PCMENTOR SZERVIZ KFT.
Város: Budapest


