Ú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!
- Samsung LCD és LED TV-k
- sziku69: Fűzzük össze a szavakat :)
- NiMH akkumulátor
- sziku69: Szólánc.
- Autós topik
- Luck Dragon: Asszociációs játék. :)
- AMD Ryzen 9 / 7 / 5 9***(X) "Zen 5" (AM5)
- Battlefield 6
- Kormányok / autós szimulátorok topikja
- Milyen asztali (teljes vagy fél-) gépet vegyek?
- További aktív témák...
- KINGSTON FURY 32GB Beast DDR5 5200MHz CL40 KIT KF552C40BBK2-32
- Apple iPhone 12 Mini 64GB, Kártyafüggetlen, 1 Év Garanciával
- Gigabyte GA-H110M-S2H PC, i5-7400 CPU, Windows 11
- DELL latitude 5410 Tartós Üzleti Laptop 14" -70% i5-8365U 4Mag 16Gb 500GB SSD FHD IPS LTE
- SZÉP! DELL latitude 5410 Tartós Üzleti Laptop 14" -70% i5-8365U 4Mag 16Gb 512GB SSD FHD IPS
- ÁRGARANCIA!Épített KomPhone i5 12400F 16/32/64GB RAM RTX 5070 12GB GAMER PC termékbeszámítással
- ÁRGARANCIA!Épített KomPhone Ryzen 7 5700X 16/32/64GB RAM RX 9060 XT 8GB GAMER PC termékbeszámítással
- ÁRGARANCIA!Épített KomPhone Ryzen 5 5600X 16/32/64GB RAM RTX 5060 8GB GAMER PC termékbeszámítással
- GYÁRI TÖLTŐK Macbook Magsafe 2 Budapest,/MPL/Foxpost
- Telefon felvásárlás!! Samsung Galaxy A20e/Samsung Galaxy A40/Samsung Galaxy A04s/Samsung Galaxy A03s
Állásajánlatok
Cég: Laptopműhely Bt.
Város: Budapest
Cég: PCMENTOR SZERVIZ KFT.
Város: Budapest


