I have to create a system with parallel processes that will use a ring buffer.The "dad" process must of course create semaphores and segment and start their sons, then he can retreat and wait for the others to finish.You will create a process that reads characters from the keyboard and writes them to the ring buffer, you create a process that generates text and puts on the ring buffer, and you create a process that reads from the ring buffer and writes on the screen. All necessary resources must be synchronized (of course).
I'm tried so far but the program is not working. Is there any who expert can help me with this.
Here is .h file:
template<typename T>
class RingBuffer
{
private:
T* _value;
int size;
int front;
int back;
bool empty;
public:
RingBuffer(int s);
~RingBuffer();
void _add(T element);
T get();
void _write(RingBuffer *_value);
void _read(RingBuffer *_value);
bool is_empty();
int get_size();
};
Here is .cpp file:
#include "RingBuffer.h"
#include <iostream>
template<typename T>
RingBuffer<T>::RingBuffer(int s)
:size(s)
{
_value = new T[size];
}
template<typename T>
RingBuffer<T>::~RingBuffer()
{
if (_value!= nullptr)
{
delete[] _value;
}
}
template<typename T>
void RingBuffer<T>::_add(T element)
{
if (back == front & !empty)
{
return;
}
_value[back] = element;
_value[back] = (back + 1) % size;
empty = false;
}
template<typename T>
T RingBuffer<T>::get()
{
if (back == front && empty)
{
return 0;
}
int return_value = _value[front];
front = (front + 1) % size;
if (front == back)
empty = true;
return true;
}
template<typename T>
void RingBuffer<T>::_read(RingBuffer *_value)
{
while (true)
{
std::cout << _value->get();
}
}
template<typename T>
void RingBuffer<T>::_write(RingBuffer* _value)
{
while (true)
{
std::string s;
std::getline(std::cin, s);
for (int i = 0; i < s.size(); i++)
{
_value->_add(s[i]);
}
std::cout << std::endl;
}
}
template<typename T>
int RingBuffer<T>::get_size()
{
return size;
}
template<typename T>
bool RingBuffer<T>::is_empty()
{
if (back == 0)
return true;
else
return false;
}
Here is main function:
#include <iostream>
#include <thread>
#include <string>
#include "RingBuffer.h"
int main(){
int write = 0;
int read = 0;
RingBuffer<std::string> _value(10);
std::thread key(write, &_value);
std::thread reader(read, &_value);
key.join();
reader.join();
system("pause");
return 0;
}
These two are the errors in get() function.
// C2672 'std::invoke': no matching overloaded function found
// Error C2893 Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...) //noexcept()'