c++ - 将类对象作为参数传递给 boost::thread

标签 c++ boost boost-thread

我有一个名为“producer”的函数,它接受一个类对象作为参数。我正在尝试使用 boost::thread 为生产者创建线程。但是,由于我作为参数传递的类对象,它导致了错误。我不知道为什么会出错。如果我删除函数参数并让它作为全局变量传入,它就可以正常工作。下面是我的代码。

阻塞队列.h

#ifndef BLOCKINGQUEUE_H_
#define BLOCKINGQUEUE_H_

#include <queue>
#include <iostream>
#include <boost/thread.hpp>

template<class T>
class BlockingQueue
{
private:
    std::queue<T>               m_queBlockingQueue;
    boost::condition_variable   m_cvSignal;
    boost::mutex                m_mtxSync;

public:
    BlockingQueue();
    bool isEmpty();
    T& popElement();
    void pushElement(T nElement);
    virtual ~BlockingQueue();
};

template<class T>
BlockingQueue<T>::BlockingQueue()
{
}

template<class T>
bool BlockingQueue<T>::isEmpty()
{
    bool bEmpty;
    boost::mutex::scoped_lock lock(m_mtxSync);
    m_cvSignal.wait(lock);
    bEmpty = m_queBlockingQueue.empty();
    return bEmpty;
}

template<class T>
T& BlockingQueue<T>::popElement()
{
    boost::mutex::scoped_lock lock(m_mtxSync);
    while (m_queBlockingQueue.empty())
    {
        m_cvSignal.wait(lock);
    }
    T& nElement = m_queBlockingQueue.front();
    m_queBlockingQueue.pop();
    return nElement;
}

template<class T>
void BlockingQueue<T>::pushElement(T nElement)
{
    boost::mutex::scoped_lock lock(m_mtxSync);
    m_queBlockingQueue.push(nElement);
    m_cvSignal.notify_one();
}

template<class T>
BlockingQueue<T>::~BlockingQueue()
{
}

#endif /* BLOCKINGQUEUE_H_ */

main.cpp

#include "BlockingQueue.h"

#include <iostream>

using namespace std;

void producer (BlockingQueue<int>& blockingQueue)
{
    for (int i=0; i<100; i++)
    {
        cout<<"Producer about to push("<<i<<")..."<<endl;
        blockingQueue.pushElement(i);
        sleep(1);
    }
}

void consumer (BlockingQueue<int>& blockingQueue)
{
    for (int i=0; i<100; i++)
    {
        cout<<"Consumer received: "<<blockingQueue.popElement()<<endl;
        sleep(3);
    }
}

int main ()
{
    BlockingQueue<int> blockingQueue;
    cout<<"Program started..."<<endl;
    cout.flush();

    boost::thread tConsumer(consumer, blockingQueue);
    boost::thread tProducer(producer, blockingQueue);

    tProducer.join();
    tConsumer.join();

    return 0;
}

我得到的错误是这样的:

1) 参数 1 没有从‘const BlockingQueue’到‘BlockingQueue&’的已知转换 2) BlockingQueue::BlockingQueue(BlockingQueue&) 在此上下文中 3) 没有匹配函数来调用‘BlockingQueue::BlockingQueue(const BlockingQueue&)’

我还有其他错误,例如: thread.hpp:148:47: 错误:正在初始化 'static boost::detail::thread_data_ptr boost::thread::make_thread_info(F) [with F = boost::_bi::bind_t&), boost::_bi 的参数 1::list1 >>>, boost::detail::thread_data_ptr = boost::shared_ptr]'

我的类中是否缺少某些函数(如复制构造函数等)或什么。如果我使用我的代码来传递像 int、double 这样的原始数据类型,它就可以正常工作。这是我的类(class)“BlockingQueue”的一些问题。

最佳答案

如果你想通过引用传递你需要使用boost::ref,或者只使用一个指针。 boost::thread 按值复制,因此不能使用按引用传递函数。例如你可以这样做:

#include "BlockingQueue.h"

#include <iostream>

using namespace std;

void producer (boost::reference_wrapper<BlockingQueue<int>> blockingQueue)
{
    for (int i=0; i<100; i++)
    {
        cout<<"Producer about to push("<<i<<")..."<<endl;
        blockingQueue.get_pointer()->pushElement(i);
        sleep(1);
    }
}

void consumer (boost::reference_wrapper<BlockingQueue<int>> blockingQueue)
{
    for (int i=0; i<100; i++)
    {
        cout<<"Consumer received: "<<blockingQueue.get_pointer()->popElement()<<endl;
        sleep(3);
    }
}

int main ()
{
    BlockingQueue<int> blockingQueue;
    cout<<"Program started..."<<endl;
    cout.flush();

    boost::thread tConsumer(consumer, ref(blockingQueue));
    boost::thread tProducer(producer, ref(blockingQueue));

    tProducer.join();
    tConsumer.join();

    return 0;
}

关于c++ - 将类对象作为参数传递给 boost::thread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12578473/

相关文章:

c++ - typedef 之间的兼容性问题

c++ - 使用升压锁进行 RAII 对信号量的访问

multithreading - 为什么Intel TBB不提供boost之类的条件变量?

C++ - 无法从函数返回 vector

c++ - 当派生定义了析构函数时,使用复制构造函数而不是移动构造函数

c++ - BOOST 库 c++ 的问题

xcode - Boost在Xcode上的线程组

c++ - 获取指针属性值

c# - 在构建机器上注册 .tlb 的首选方法是什么?

c++ - 将语义 Action 附加到具有 boost spirit 的解析器