【问题标题】:error C2248 while using std::atomic<bool>::atomic使用 std::atomic<bool>::atomic 时出现错误 C2248
【发布时间】:2014-07-21 15:25:04
【问题描述】:

首先,请原谅我发了这么长的帖子。

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

我收到以下错误:

请参考下面的代码。

它基于 boost 文档中的一个示例。 (无等待单生产者/单消费者队列)

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 消息并将它们推送到队列中。

消费者线程读取队列并处理这些消息。到目前为止,我只是显示会话 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;

}

【问题讨论】:

    标签: c++ multithreading boost fix-protocol


    【解决方案1】:

    std::atomics are not copyable.

    因此,不可能将它们按值传递给函数。

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

    【讨论】:

    • 啊,我明白了。将它作为指针传递就可以了。但仍然想了解,当我传递对它的引用时 producerThread(producer, pqFixMessages, ref(running)); - 它是创建它的副本还是对同一个对象进行操作?
    • std::ref 只在为线程创建闭包时不复制对象。执行线程的函数仍然不能通过值获取它的参数,否则你会在启动线程时将它复制到这里。只有在创建线程时同时将函数参数作为引用 std::ref 时,才能获得真正的传递引用。
    • 是的 - 这是有道理的。通过引用传递它们都有效。这是一个错误。 void producer(spsc_queue<:message capacity>> * pqFixMessages, std::atomic running) 应该是 void producer(spsc_queue<:message capacity>> * pqFixMessages, std::原子&运行)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-22
    • 2019-11-11
    • 2012-10-19
    • 1970-01-01
    • 2017-02-13
    • 2015-08-25
    • 2017-11-24
    相关资源
    最近更新 更多