less copy and less lock

This commit is contained in:
Domingo Dirutigliano
2025-02-17 13:07:06 +01:00
parent 7f7e3353ec
commit 5a1e1bc225
6 changed files with 106 additions and 67 deletions

View File

@@ -22,6 +22,36 @@ bool unhexlify(std::string const &hex, std::string &newString) {
}
}
#ifdef USE_PIPES_FOR_BLOKING_QUEUE
template<typename T>
class BlockingQueue
{
private:
int pipefd[2];
public:
BlockingQueue(){
if (pipe(pipefd) == -1) {
throw std::runtime_error("pipe");
}
}
void put(T new_value)
{
if (write(pipefd[1], &new_value, sizeof(T)) == -1) {
throw std::runtime_error("write");
}
}
void take(T& value)
{
if (read(pipefd[0], &value, sizeof(T)) == -1) {
throw std::runtime_error("read");
}
}
};
#else
template<typename T, int MAX = 1024> //same of kernel nfqueue max
class BlockingQueue
{
@@ -32,6 +62,7 @@ private:
std::condition_variable condNotFull;
size_t count; // Guard with Mutex
public:
void put(T new_value)
{
@@ -61,4 +92,6 @@ public:
}
};
#endif
#endif // UTILS_CPP