简体   繁体   English

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

[英]Passing class object as argument to boost::thread

I have a function called "producer" that takes a class object as argument. 我有一个名为“producer”的函数,它将一个类对象作为参数。 I am trying to create thread for producer using boost::thread. 我正在尝试使用boost :: thread为生产者创建线程。 However it is causing error because of the class object that I am passing as argument. 但是由于我作为参数传递的类对象,它导致错误。 I cannot figure out why is it giving error. 我无法弄清楚为什么会出错。 If I remove function arguments and let it pass in as global variable, it works fine. 如果我删除函数参数并让它作为全局变量传入,它可以正常工作。 Below is my code. 以下是我的代码。

BlockingQueue.h 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 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;
}

The errors that I am getting are something like: 我得到的错误是这样的:

1) no known conversion for argument 1 from 'const BlockingQueue' to 'BlockingQueue&' 2) BlockingQueue::BlockingQueue(BlockingQueue&) within this context 3) no matching function for call to 'BlockingQueue::BlockingQueue(const BlockingQueue&)' 1)参数1从'const BlockingQueue'到'BlockingQueue&'没有已知的转换2)BlockingQueue :: BlockingQueue(BlockingQueue&)在此上下文中3)没有匹配函数用于调用'BlockingQueue :: BlockingQueue(const BlockingQueue&)'

I have other errors like: thread.hpp:148:47: error: initializing argument 1 of 'static boost::detail::thread_data_ptr boost::thread::make_thread_info(F) [with F = boost::_bi::bind_t&), boost::_bi::list1 > > >, boost::detail::thread_data_ptr = boost::shared_ptr]' 我有其他错误,如:thread.hpp:148:47:错误:初始化'static boost :: detail :: thread_data_ptr boost :: thread :: make_thread_info(F)的参数1 [与F = boost :: _ bi :: bind_t& ),boost :: _ bi :: list1 >>>,boost :: detail :: thread_data_ptr = boost :: shared_ptr]'

Is there some function like copy constructor etc in my class which is missing or what. 在我的班级中是否有一些像复制构造函数等功能缺失或者是什么。 If I use my code to pass primitive data types like int, double it works fine. 如果我使用我的代码传递原始数据类型,如int,double它工作正常。 It was some issue with my class "BlockingQueue". 这是我的班级“BlockingQueue”的一些问题。

If you want to pass by reference you need to use boost::ref, or just use a pointer. 如果你想通过引用传递,你需要使用boost :: ref,或者只使用指针。 boost::thread copies by value, and therefore cannot use your pass by reference function. boost :: thread按值复制,因此不能使用pass by reference函数。 For instance you could do: 例如,您可以这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM