C++ 将类对象作为参数传递给boost::thread
我有一个名为“producer”的函数,它将类对象作为参数。我正在尝试使用boost::thread为producer创建线程。但是,由于我作为参数传递的类对象,它会导致错误。我不明白为什么它会出错。如果我删除函数参数并让它作为全局变量传入,它就可以正常工作。下面是我的代码 阻塞队列C++ 将类对象作为参数传递给boost::thread,c++,boost,boost-thread,C++,Boost,Boost Thread,我有一个名为“producer”的函数,它将类对象作为参数。我正在尝试使用boost::thread为producer创建线程。但是,由于我作为参数传递的类对象,它会导致错误。我不明白为什么它会出错。如果我删除函数参数并让它作为全局变量传入,它就可以正常工作。下面是我的代码 阻塞队列 #ifndef BLOCKINGQUEUE_H_ #define BLOCKINGQUEUE_H_ #include <queue> #include <iostream> #includ
#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_ */
\ifndef阻塞队列\u H_
#定义阻塞队列_
#包括
#包括
#包括
模板
类阻塞队列
{
私人:
std::队列m_queBlockingQueue;
boost::条件变量m_cvSignal;
mutexm_mtxSync;
公众:
阻塞队列();
bool是空的();
T&popElement();
空心单元(T单元);
虚拟~BlockingQueue();
};
模板
BlockingQueue::BlockingQueue()
{
}
模板
bool BlockingQueue::isEmpty()
{
布尔空荡荡的;
boost::mutex::作用域锁定(m_mtxSync);
m_cvSignal.等待(锁定);
bEmpty=m_queBlockingQueue.empty();
还空着;
}
模板
阻塞队列::popElement()
{
boost::mutex::作用域锁定(m_mtxSync);
while(m_queBlockingQueue.empty())
{
m_cvSignal.等待(锁定);
}
T&neelement=m_queBlockingQueue.front();
m_queBlockingQueue.pop();
返回元素;
}
模板
void BlockingQueue::pushElement(T neElement)
{
boost::mutex::作用域锁定(m_mtxSync);
m_queBlockingQueue.push(NEElement);
m_cvSignal.notify_one();
}
模板
BlockingQueue::~BlockingQueue()
{
}
#endif/*阻塞队列*/
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;
}
#包括“BlockingQueue.h”
#包括
使用名称空间std;
无效生成程序(BlockingQueue和BlockingQueue)
{
对于(int i=0;i如果要通过引用传递,则需要使用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;
}
#包括“BlockingQueue.h”
#包括
使用名称空间std;
无效生成器(boost::reference\u包装器阻塞队列)
{
for(int i=0;如果您不阻止它,则会隐式生成icopy构造函数以获得快速回复。如果我复制,请使用较小的更改粘贴代码(boost::ref()而不是ref()),我得到以下错误:../BlockingQueue_test.cpp:15:58:错误:'>>'应该在嵌套模板参数列表中'>>。/BlockingQueue_test.cpp:25:58:错误:'>>'应该在嵌套模板参数列表中'>>''bind/bind.hpp:253:9:错误:无法转换'(&a)->boost:.\u bi::list0::operator[][with T=BlockingQueue]((const boost::reference_wrapper)(&((boost::_bi::list1