Firstly, pardon me for the long post.
I am using boost::lockfree::spsc_queue to run on two separate threads to process FIX messages. I am using quickfix for converting FIX strings from a file to convert to FIX Messages. I want to be able to pass the queue as a pointer to both threads and a boolean value that indicates whether there are still messages to be processes.
I am getting the following error:
Please refer the code below.
It is based of an example in boost documentation. (Waitfree Single-Producer/Single-Consumer Queue)
http://www.boost.org/doc/libs/1_55_0/doc/html/lockfree/examples.html
I have been trying different ways to transfer the value of running and pqFixMessages to the two threads but nothing seams to work as of now. I will appreciate any suggestions.
'std::atomic<bool>::atomic' : cannot access private member declared in class 'std::atomic<bool>
Description:
Producer thread reads a file, creates FIX messages and pushes them in the queue.
Consumer thread reads the queue and processes these messages. As of now, I am just displaying the session id's for debugging.
Main has a pointer to the queue that is passed on to both the threads.
Further Context: After this works, I want producer and consumer to be separate classes.
#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
s are not copyable.
Hence, passing them by value to a function is not possible.
This is for a reason. Usually, attempting to pass them by value indicates a programming error. They are typically used as synchronization primitives and you cannot synchronize anything with a copy.