0

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()'

Sailo
  • 1
  • 1
  • 3
    _"is not working."_ isn't a concise error description. [Edit] your question please. – πάντα ῥεῖ Aug 31 '19 at 14:12
  • Hi! I have an error in get function in Ringbuffer.cpp file. I have also posted now what the error says.. – Sailo Aug 31 '19 at 16:52
  • Why are you passing a pointer to a ring buffer to the ring buffer's `read` and `write` methods? I would expect that the `read` and `write` method operate on the internal (member) data structure rather than some other structure (by the way, pointers can point to anywhere, including invalid locations). – Thomas Matthews Aug 31 '19 at 18:04
  • Thomas Mathews It would be absolutely big help if you wrote the code down for that... – Sailo Aug 31 '19 at 18:52

1 Answers1

0

You cannot have your template definitions in the .cpp file.

Look here (more suited to your question) or here for more info on that.

eike
  • 1,314
  • 7
  • 18