gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 03:31:11 27 4
gpt4 key购买 nike

我有一个名为“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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com