P
P
Pavel2015-12-31 10:28:11
C++ / C#
Pavel, 2015-12-31 10:28:11

Where is the error in the implementation of the thread-safe queue template class?

Hello. The compiler throws 'error: undefined reference to `threadsafe_queue::push(int)'
in the following code:

//threadsafe_queue.h
#ifndef THREADSAFE_QUEUE_H
#define THREADSAFE_QUEUE_H

#include <mutex>
#include <condition_variable>
#include <queue>
#include <memory>

template<typename T>
class threadsafe_queue
{
private:
    mutable std::mutex mut;
    std::queue<T> data_queue;
    std::condition_variable data_cond;

public:
    threadsafe_queue();
    threadsafe_queue(const threadsafe_queue &other);

    void push(T new_value);

    void wait_and_pop(T& value);

    std::shared_ptr<T> wait_and_pop();

    bool try_pop(T& value);

    std::shared_ptr<T> try_pop();

    bool empty() const;
};

#endif // THREADSAFE_QUEUE_H

//threadsafe_queue.cpp
#include "threadsafe_queue.h"

template<typename T>
threadsafe_queue<T>::threadsafe_queue()
{}

template<typename T>
threadsafe_queue<T>::threadsafe_queue(const threadsafe_queue &other)
{
    std::lock_guard<std::mutex> lk(other.mut);
    data_queue=other.data_queue;
}

template<typename T>
void threadsafe_queue<T>::push(T new_value)
{
    std::lock_guard<std::mutex> lk(mut);
    data_queue.push(new_value);
    data_cond.notify_one();
}

template<typename T>
void threadsafe_queue<T>::wait_and_pop(T &value)
{
    std::unique_lock<std::mutex> lk(mut);
    data_cond.wait(lk,[this]{return !data_queue.empty();});
    value=data_queue.front();
    data_queue.pop();
}

template<typename T>
std::shared_ptr<T> threadsafe_queue<T>::wait_and_pop()
{
    std::unique_lock<std::mutex> lk(mut);
    data_cond.wait(lk,[this]{return !data_queue.empty();});
    std::shared_ptr<T> res(std::make_shared<T>(data_queue.front()));
    data_queue.pop();
    return res;
}

template<typename T>
bool threadsafe_queue<T>::try_pop(T &value)
{
    std::lock_guard<std::mutex> lk(mut);
    if(data_queue.empty)
        return false;
    value=data_queue.front();
    data_queue.pop();
}

template<typename T>
std::shared_ptr<T> threadsafe_queue<T>::try_pop()
{
    std::lock_guard<std::mutex> lk(mut);
    if(data_queue.empty())
        return std::shared_ptr<T>();
    std::shared_ptr<T> res(std::make_shared<T>(data_queue.front()));
    data_queue.pop();
    return res;
}

template<typename T>
bool threadsafe_queue<T>::empty() const
{
    std::lock_guard<std::mutex> lk(mut);
    return data_queue.empty();
}

//main.cpp
threadsafe_queue<int> q;

void AddData()
{
    int sec = 0;
    int sec_for_sleep = 2;
    while(true) {
        std::this_thread::sleep_for(std::chrono::milliseconds(sec_for_sleep * 1000));
        sec += sec_for_sleep;
        q.push(sec); //здесь указывает на ошибку
        if (sec > 30) break;
    }
}
int main()
{
return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Armenian Radio, 2015-12-31
@PavelG94

Yes, how many bayan mistakes can you make?
Template bodies - only in *.h files.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question