1
0
Fork 0
This repository has been archived on 2021-11-03. You can view files and clone it, but cannot push or open issues or pull requests.
uni-m.cds-adv-prog/ex10/shared_queue.hh

40 lines
764 B
C++
Raw Normal View History

2020-07-08 16:53:53 +02:00
// Shared_Queue.hh
#include <condition_variable>
2020-07-09 21:22:39 +02:00
#include <iostream>
2020-07-08 16:53:53 +02:00
#include <mutex>
#include <queue>
#ifndef SHARED_QUEUE_HH
#define SHARED_QUEUE_HH
template <class T>
class SharedQueue
{
2020-07-09 18:09:10 +02:00
public:
SharedQueue() :
_q(),
_q_mutex(),
_q_cv()
{};
2020-07-08 16:53:53 +02:00
2020-07-09 18:09:10 +02:00
SharedQueue( const SharedQueue& other )
{
2020-07-09 21:22:39 +02:00
std::cout << "Copy Constructor SharedQueue" << std::endl;
2020-07-09 18:09:10 +02:00
// (Read)Lock the other queue for copying
2020-07-09 21:22:39 +02:00
std::unique_lock<std::mutex> other_lock(other._q_mutex);
2020-07-09 18:09:10 +02:00
_q = other._q;
}
std::queue<T> * queue() { return& _q; }
std::mutex * mutex() { return& _q_mutex; }
std::condition_variable * cv() { return& _q_cv; }
private:
// Queue
std::queue<T> _q;
// Synchronisation
2020-07-09 21:22:39 +02:00
std::mutex _q_mutex;
std::condition_variable _q_cv;
2020-07-08 16:53:53 +02:00
};
#endif