[英]error C2248 while using std::atomic<bool>::atomic
Firstly, pardon me for the long post. 首先,请原谅我。
I am using boost::lockfree::spsc_queue to run on two separate threads to process FIX messages. 我正在使用boost :: lockfree :: spsc_queue在两个单独的线程上运行以处理FIX消息。 I am using quickfix for converting FIX strings from a file to convert to FIX Messages.
我正在使用quickfix从文件转换FIX字符串以转换为FIX消息。 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. 它基于boost文档中的示例。 (Waitfree Single-Producer/Single-Consumer Queue)
(Waitfree单生产者/单消费者队列)
http://www.boost.org/doc/libs/1_55_0/doc/html/lockfree/examples.html 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. 我一直在尝试不同的方法来将running和pqFixMessages的值传递给两个线程,但是到目前为止没有任何缝隙可以工作。 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. 生产者线程读取文件,创建FIX消息并将其推送到队列中。
Consumer thread reads the queue and processes these messages. 使用者线程读取队列并处理这些消息。 As of now, I am just displaying the session id's for debugging.
到目前为止,我仅显示会话ID以进行调试。
Main has a pointer to the queue that is passed on to both the threads. Main具有指向传递给两个线程的队列的指针。
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. std::atomic
不可复制。
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.
它们通常用作同步原语,并且您无法将任何内容与副本同步。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.