- Házimozi haladó szinten
- Milyen TV-t vegyek?
- Házi barkács, gányolás, tákolás, megdöbbentő gépek!
- iPad topik
- Azonnali informatikai kérdések órája
- Nagyon érzékeny lett a játékok archiválására a Nintendo
- Apple MacBook
- Iszonyatos mennyiségű hulladékkal járhat a Windows 10 terméktámogatásának vége
- AMD vs. INTEL vs. NVIDIA
- Mégis mehetnek Kínába az áprilisban korlátozás alá helyezett AI gyorsítók
Ú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!
- Hegesztés topic
- Windows 10
- Házimozi haladó szinten
- Milyen TV-t vegyek?
- Házi barkács, gányolás, tákolás, megdöbbentő gépek!
- Nintendo 3DS és DS topic
- Kodi és kiegészítői magyar nyelvű online tartalmakhoz (Linux, Windows)
- Abarth, Alfa Romeo, Fiat, Lancia topik
- Nagyrobogósok baráti topikja
- Folyószámla, bankszámla, bankváltás, külföldi kártyahasználat
- További aktív témák...
- Gigabyte B450M S2H + Ryzen 5 1400 kisebb-nagyobb hibával
- Nokia 105 4G (2023) charcoal, Nokia 110 4G (2023) midnight blue
- ASRock B550 PG Velocita + Ryzen 5 3600 + 32GB (4x8GB) DDR4 3600Mhz CL18
- Philips 58PUS8505 Smart LED Televízió,146 cm, 4K Ultra HD ,Android, Ambilight, HDR10+ KIJELZŐHIBÁSAN
- Canon EOS 250D kiegészítőkkel, táskával (CSAK 200 expoval !!! )
- Új! Targus - USB-C Dual HDMI 4K HUB - 2 HDMI-vel. Saját töltő nélkül 2 monitorral (120Hz)
- OLCSÓBB!!! több EIZO EV2456 FlexScan 24" 1920x1200 16:10 IPS fekete több jelenlegi ár: 170.000.-!!!
- DELL PowerEdge R440 rack szerver - Silver 4114 (10 mag/20 szál, 2.2/3.0GHz), 64GB RAM,199990Ft +ÁFA
- RAKTÁRSÖPRÉS!!! - Videókártyák, Monitorok, Notebookok, Stb. - Szaküzletből! Számlával!
- Csere-Beszámítás! Asztali számítógép PC Játékra! I5 12400F / RTX 3070 / 32GB DDR4 / 500GB SSD
Állásajánlatok
Cég: PCMENTOR SZERVIZ KFT.
Város: Budapest