首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用C2248时出错std::atomic<bool>::atomic

使用C2248时出错std::atomic<bool>::atomic
EN

Stack Overflow用户
提问于 2014-07-21 15:25:04
回答 1查看 1.1K关注 0票数 0

首先,请原谅我做了这么长的工作。

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

我得到了以下错误:

请参考下面的代码。

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

0/doc/html/lockfree/examples.html

我一直在尝试不同的方法将运行和pqFixMessages的值传递给这两个线程,但到目前为止还没有任何接缝可以工作。如有任何建议,我将不胜感激。

代码语言:javascript
复制
'std::atomic<bool>::atomic' : cannot access private member declared in class 'std::atomic<bool>

描述:

生产者线程读取文件,创建修复消息并将它们推入队列中。

使用者线程读取队列并处理这些消息。到目前为止,我只是显示会话id以进行调试。

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

进一步的背景:在这个作品之后,我希望生产者和消费者是分开的类。

代码语言:javascript
复制
#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;

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-21 15:36:17

s are not copyable.

因此,通过值将它们传递给函数是不可能的。

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24868840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档