簡體   English   中英

使用std :: atomic時發生錯誤C2248 <bool> ::原子

[英]error C2248 while using std::atomic<bool>::atomic

首先,請原諒我。

我正在使用boost :: lockfree :: spsc_queue在兩個單獨的線程上運行以處理FIX消息。 我正在使用quickfix從文件轉換FIX字符串以轉換為FIX消息。 我希望能夠將隊列作為兩個線程的指針和一個布爾值傳遞,該值指示是否仍有消息要進行處理。

我收到以下錯誤:

請參考下面的代碼。

它基於boost文檔中的示例。 (Waitfree單生產者/單消費者隊列)

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;

}

std::atomic不可復制。

因此,無法將它們按值傳遞給函數。

這是有原因的。 通常,嘗試按值傳遞它們表示編程錯誤。 它們通常用作同步原語,並且您無法將任何內容與副本同步。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM