【发布时间】: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