클래스 객체를 인수로 취하는 "producer"라는 함수가 있습니다. boost :: thread를 사용하여 제작자 용 스레드를 만들려고합니다. 그러나 그것은 인수로 전달 오전 클래스 개체 때문에 오류가 발생합니다. 나는 왜 그것이 오류를 일으키는 지 알 수 없다. 함수 인수를 제거하고 전역 변수로 전달하면 잘 동작합니다. 아래는 제 코드입니다. 내가 얻고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) 'const를 BlockingQueue의에서 인수 1 알려진 변환 이 (C) 내에서의 BlockingQueue & ' 2)의 BlockingQueue :: BlockingQueue의 (BlockingQueue를 &)'에서 ' 3)에 호출 '의 BlockingQueue :: BlockingQueue의 (CONST BlockingQueue를 &)'에 대한 정합 기능 타세
I이 다른 에러 같은 thread.hpp : 148 : 47 : 오류 '정적 부스트 인자 1 초기화 : : detail :: thread_data_ptr boost :: _ :: bi :: list1>>>, boost :: detail :: thread_data_ptr = boost :: thread_data_ptr boost :: _ :: thread_data_ptr (boost :: _ bi :: bind_t &) : shared_ptr의] '
가 없습니다 내 클래스 복사 생성자 등 또는 무엇과 같은 몇 가지 기능이있다. 내 코드를 사용하여 int와 같은 원시 데이터 유형을 전달하면 double이 정상적으로 작동합니다. 그것은 내 수업 "BlockingQueue"와 함께 몇 가지 문제가되었습니다. 참조로 전달하려는 경우
복사 생성자는 차단하지 않으면 암시 적으로 생성됩니다. –