Hirdetés
- Magga: PLEX: multimédia az egész lakásban
- GoodSpeed: Windows 11 - HP Pavilion – 15 cc516nh (2LE41EA) laptopon Intel I3-7100U TPM2.0
- gerner1
- Brogyi: CTEK akkumulátor töltő és másolatai
- ubyegon2: Airfryer XL XXL forrólevegős sütő gyakorlati tanácsok, ötletek, receptek
- sziku69: Szólánc.
- Luck Dragon: Asszociációs játék. :)
- Gurulunk, WAZE?!
- sziku69: Fűzzük össze a szavakat :)
- Geri Bátyó: Agglegénykonyha 7 – Még egy megosztó – de gyors – étel: resztelt máj
Ú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
● ha kódot szúrsz be, használd a PROGRAMKÓD formázási funkciót!
- Magga: PLEX: multimédia az egész lakásban
- Vezeték nélküli fejhallgatók
- OpenMediaVault
- iPhone topik
- Futás, futópályák
- Samsung Galaxy S25 Ultra - titán keret, acélos teljesítmény
- Sebtapasz: telepíthető az iOS 26.0.1
- Kerékpárosok, bringások ide!
- Nagy csokor frissítéssel kíván jó hétvégét a Samsung
- Háztartási gépek
- További aktív témák...
- Garmin MARQ Adventurer prémium kategóriás okosóra full szett. eredeti árához képest HARMAD áron!
- Lenovo ThinkPad T14s Gen 2 i5 / 16 GB / 256 GB SSD + Windows 11
- ÚJ BONTATLAN Apple Watch Series 10 S10 42-46mm Azonnal Átvehető DEÁK TÉRNÉL 1 Év Apple Garanciával.
- Honor Magic 7 Pro 512 Gb - AI Pro kamera, 6,8 120 Hz LTPO OLED, Snapdragon 8 Elite,3 hó gari!
- Samsung Galaxy Z Fold6 Navy Duplakijelzős produktivitás, 120 Hz, Galaxy AI,2027. 09. 19
- Törött Apple iMac 19.2 i5-8500 Radeon Pro 560X 4GB 16GB 256GB SSD 21.5" 4K Retina
- Bomba ár! Dell Latitude E5440 - i5-4GEN I 4-8GB I HDD v SSD I 14" I HDMI I Cam I W10 I Gari!
- Lenovo Legion Slim 5 16AHP9
- Gamer PC-Számítógép! Csere-Beszámítás! Mini PC! I5 10600KF / RTX 3060 12GB/ 16GB DDR4 / 1TB SSD
- Lenovo ThinkPad X13 G2 multitouch (minimálisan használt)
Állásajánlatok
Cég: PCMENTOR SZERVIZ KFT.
Város: Budapest
Cég: Laptopműhely Bt.
Város: Budapest