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

时间:2012-09-25 07:50:50

标签: c++ boost boost-thread

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

BlockingQueue.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&amp;'没有已知的转换 2)在此上下文中阻塞Queue :: BlockingQueue(BlockingQueue&amp;) 3)没有匹配函数来调用'BlockingQueue :: BlockingQueue(const BlockingQueue&amp;)'

我还有其他错误: thread.hpp:148:47:错误:初始化'static boost :: detail :: thread_data_ptr boost :: thread :: make_thread_info(F)[with F = boost :: _ bi :: bind_t&amp;)的参数1,boost :: _bi :: list1&gt; &GT; &gt;,boost :: detail :: thread_data_ptr = boost :: shared_ptr]'

在我的班级中是否有一些像复制构造函数等功能缺失或者是什么。如果我使用我的代码传递原始数据类型,如int,double它工作正常。这是我的班级“BlockingQueue”的一些问题。

1 个答案:

答案 0 :(得分:2)

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

#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;
}