- sziku69: Szólánc.
- Luck Dragon: Asszociációs játék. :)
- sh4d0w: Netflix? Ugyan, VW előfizetés!
- sziku69: Fűzzük össze a szavakat :)
- Geri Bátyó: Megint tahó voltam – SZEMÉLYISÉGFEJLŐDÉS
- Mr Dini: Mindent a StreamSharkról!
- Meggyi001: Nyilvános wc-k.....még mindig hiánypótló...
- GoodSpeed: AMD Ryzen 7 7700X vs AMD Ryzen 9 9900X Cinebench R23 & R24 Benchmarkokban mérve
- D1Rect: Nagy "hülyétkapokazapróktól" topik
- Meggyi001: RTX 5060 - Az új népkártya?
Új hozzászólás Aktív témák
-
cog777
senior tag
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
Hirdetés
● ha kódot szúrsz be, használd a PROGRAMKÓD formázási funkciót!
- Bomba ár! HP Pavilion Gaming - i7-8750H I 16GB I 512SSD I GTX1050 I 15,6" FHD I Cam I W11 I Gari!
- Asztali PC , R7 5800X , RTX 2080 , 32GB DDR4 , 960GB NVME
- Bomba ár! HP Pavilion 15 - i7-1065G7 I 16GB I 512SSD I Nvidia GTX1050 I 15,6" FHD I Cam I W11 I Gar
- Bomba ár! HP Pavilion 15 - i7-8565U I 16GB I 512SSD I Nvidia GTX1050 I 15,6" FHD I Cam I W11 I Gari!
- Bomba ár! HP Pavilion 15 - i5-8265U I 8GB I 256SSD + 1TB I Nvidia I 15,6" FHD I Cam I W11 I Gari!
- Eladó Apple Mac Mini 2012 vége / 12 hó tótállás
- HP ZBook Studio G7 i7-10850H 32GB 512GB Nvidia Quadro T1000 15.6" FHD 1 év garancia
- Bomba ár! Dell Latitude 7390 2in1 - i7-8G I 16GB I 256SSD I 13,3"FHD Touch I HDMI I Cam I W11 I Gar
- billentyűzetek - kiárusítás - Logitech, Corsair, ASUS
- HP EliteBook 840 G8 i5-1135G7 32GB 1000GB 1 év garancia
Állásajánlatok
Cég: FOTC
Város: Budapest