2012-09-25 1 views
0

클래스 객체를 인수로 취하는 "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"와 함께 몇 가지 문제가되었습니다. 참조로 전달하려는 경우

+0

복사 생성자는 차단하지 않으면 암시 적으로 생성됩니다. –

답변

2

당신은 부스트 ​​:: 심판을 사용하거나 포인터를 사용해야합니다. 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; 
} 
+0

빠른 답장을 보내 주셔서 감사합니다. 내가 (대신 REF()의) 부스트 :: REF() 사소한 변화에 코드를 붙여 복사하는 경우, 나는 다음과 같은 오류를 얻을 : ../BlockingQueue_test.cpp:15:58 : 오류 : '>>'해야 수 '>>'중첩 템플릿 인수 목록 ../BlockingQueue_test.cpp:25:58 내 : 오류 : '>>'해야 '>>'중첩 템플릿 인수 목록에서 /bind/bind.hpp : 253 : 9 : 오류 -> 부스트 : _ 쌍방향 ::리스트 0 :: 연산자 []와 T = BlockingQueue의 ((* (부스트 : CONST reference_wrapper > *) (A) '로 변환 할 수 없습니다 (& ((boost :: _ bi :: list1 등등 ... – Haider