使用std :: atomic <bool> :: atomic </bool>时出现错误C2248

时间:2014-07-21 15:25:05

标签: c++ multithreading boost fix-protocol

首先,请原谅我的长篇文章。

我使用boost :: lockfree :: spsc_queue在两个独立的线程上运行来处理FIX消息。我正在使用quickfix从文件转换FIX字符串以转换为FIX消息。我希望能够将队列作为指向两个线程的指针和一个布尔值传递,该值指示是否仍有消息要进程。

我收到以下错误:

请参阅以下代码。

它基于boost文档中的示例。 (Waitfree Single-Producer / Single-Consumer Queue)

http://www.boost.org/doc/libs/1_55_0/doc/html/lockfree/examples.html

我一直在尝试不同的方法将running和pqFixMessages的值传递给两个线程但是现在没有任何接缝可以工作。我将不胜感激任何建议。

'std::atomic<bool>::atomic' : cannot access private member declared in class 'std::atomic<bool>

说明

生产者线程读取文件,创建FIX消息并将其推送到队列中。

Consumer线程读取队列并处理这些消息。截至目前,我只是显示会话ID以进行调试。

Main有一个指向队列的指针,该队列传递给两个线程。

进一步的上下文:在此之后,我希望生产者和消费者成为独立的类。

#include <iostream>
#include <thread>
#include <atomic>
#include <fstream>
#include <quickfix\Message.h>
#include <boost\lockfree\spsc_queue.hpp>


using namespace std;
using namespace boost::lockfree;

void producer(spsc_queue<FIX::Message, capacity<1024>> * pqFixMessages, std::atomic<bool> running) {
    std::string line;
    std::ifstream fixMessageStream(<filename>);
    FIX::Message currentMessage;
    while (fixMessageStream) {
        std::getline(fixMessageStream, line);
        try {
            // Erases the timestamp on messages
            currentMessage = FIX::Message(line.erase(0, 25));
            pqFixMessages->push(currentMessage);
        } catch (std::exception& ex) {
        }
    }
    running = false;
}

std::atomic<bool> done(false); 

void consumer(spsc_queue<FIX::Message, capacity<1024>> * pqFixMessages, std::atomic<bool> running) {
    FIX::Message frontOfTheQueueMessage;
    while(!pqFixMessages->empty() || running) {
        if (!pqFixMessages->empty()) {
            pqFixMessages->pop(frontOfTheQueueMessage);
            cout << frontOfTheQueueMessage.getSessionID() << endl;
        }
    }
}

int main(int argc, char * argv[]) {

    spsc_queue<FIX::Message, capacity<1024>> * pqFixMessages = 
        new spsc_queue<FIX::Message, capacity<1024>>();

    std::atomic<bool> running(true);

    thread producerThread(producer, pqFixMessages, ref(running));
    cout << "Entered Producer Thread" << endl;
    thread consumerThread(consumer, pqFixMessages, ref(running));
    cout << "Entered Consumer Thread" << endl;

    producerThread.join();
    cout << "Joined Producer Thread" << endl;
    done = true;
    consumerThread.join();
    cout << "Joined Consumer Thread" << endl;

    delete pqFixMessages;

    std::cin.get();

    return 0;

}

1 个答案:

答案 0 :(得分:2)

std::atomics are not copyable.

因此,无法通过值将它们传递给函数。

这是有原因的。通常,尝试按值传递它们表示编程错误。它们通常用作同步原语,您无法通过副本同步任何内容。