@PavelG94

Где ошибка в реализации шаблонного класса потокобезопасной очереди?

Здравствуйте. Компилятор выдает 'error: undefined reference to `threadsafe_queue::push(int)'
в следующем коде:
//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;
}
  • Вопрос задан
  • 365 просмотров
Решения вопроса 1
gbg
@gbg Куратор тега C++
Любые ответы на любые вопросы
Да сколько можно баянные ошибки делать?

Тела шаблонов - только в *.h файлах.
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы