0

我使用 Boost 线程库编写了一些多线程代码。我使用占位符 _1 作为成员函数 fillSample(int num) 所需的参数在构造函数中初始化了两个线程。但这在我的 Visual Studio 2010 中无法编译。以下是代码:

#include<boost/thread.hpp>
#include<boost/thread/condition.hpp>
#include<boost/bind/placeholders.hpp>

#define SAMPLING_FREQ 250
#define MAX_NUM_SAMPLES 5*60*SAMPLING_FREQ
#define BUFFER_SIZE 8

class ECG
{
private:
    int sample[BUFFER_SIZE];
    int sampleIdx;
    int readIdx, writeIdx;
    boost::thread m_ThreadWrite;
    boost::thread m_ThreadRead;
    boost::mutex m_Mutex;
    boost::condition bufferNotFull, bufferNotEmpty;
public:
    ECG();
    void fillSample(int num);   //get sample from the data stream
    void processSample();   //process ECG sample, return the last processed 

};


ECG::ECG() : readyFlag(false), sampleIdx(0), readIdx(0), writeIdx(0)
{
    m_ThreadWrite=boost::thread((boost::bind(&ECG::fillSample, this, _1)));
    m_ThreadRead=boost::thread((boost::bind(&ECG::processSample, this)));
}

void ECG::fillSample(int num)
{
    boost::mutex::scoped_lock lock(m_Mutex);
    while( (writeIdx-readIdx)%BUFFER_SIZE == BUFFER_SIZE-1 )
    {
        bufferNotFull.wait(lock);
    }

    sample[writeIdx] = num;
    writeIdx = (writeIdx+1) % BUFFER_SIZE;
    bufferNotEmpty.notify_one();

}

void ECG::processSample()
{
    boost::mutex::scoped_lock lock(m_Mutex);
    while( readIdx == writeIdx )
    {
        bufferNotEmpty.wait(lock);
    }

    sample[readIdx] *= 2;
    readIdx = (readIdx+1) % BUFFER_SIZE;
    ++sampleIdx;
    bufferNotFull.notify_one();
}

我已经包含了 placeholders.hpp 头文件,但它仍然无法编译。如果我用 0 替换 _1,那么它将起作用。但这会用 0 初始化线程函数,这不是我想要的。关于如何使这项工作的任何想法?

4

1 回答 1

0

将创建移动到初始化列表: m_ThreadWrite(boost::bind(&ECG::fillSample, this, _1)), ...

threadobject 不可复制,并且您的编译器不支持其移动构造函数。

于 2012-09-14T18:50:08.993 回答