gpt4 book ai didi

c++ - 多线程和共享资源:使用C++定期将数据从缓冲区(数据结构)复制到文件

转载 作者:行者123 更新时间:2023-12-02 10:24:52 27 4
gpt4 key购买 nike

我的代码具有一个数据结构,例如“字符串 vector 的 vector 。我有2个线程:

线程1正在将数据写入此数据结构(RAM中的缓冲区)。

并行运行的线程2应该每隔“x”毫秒将数据从上述缓冲区(即数据结构)复制到文件中。

I'm wondering how would I achieve this in C++ ? It should consider key points in problem statement like:

a) The copy from buffer to file should happen only once in "X" miliseconds.

b) Synchronization between both threads.



在每次查询中编辑更多详细信息的查询

我想构建一个库(* .lib),该库公开了一些API,因此它从EXE或通过这些API使用我的库的任何实体获取输入数据。
假设我的图书馆收到的数据是字符串 vector 的形式。
FillLibraryDataStructure(std::vector<std::string>); // is the API of this library. Any app can call this API & pass a vector of string to this library.

Example app code:
for(int i=100; i<100;i))
{
std::vector<std::string> vec = GetVectorOfString(); // GetVectorOfString from business logic
FillLibraryDataStructure(vec);
}

库代码具有共享资源:

    // Within library I've a 2D vector i.e. vector of vector of
strings where all the vector of strings passed by application to this librray are added as a new row in vecofvecofstr.


共享资源:
std::vector<std::vector<string>> vecofvecofstr;

线程1:正在将从API接收的数据复制到数据结构,即字符串 vector 的vector。
vecofvecofstr.push_back(vec);

THREAD 2:正在将字符串 vector 的 vector 的内容(在第一个线程中写入)复制到文件(XML,HTML等)中。
每“X”毫秒。


Few more points about thread1: 1) Thread 1 should be running continuously i.e. as and when application calls the API the data received should be put to the data structure vecofvecofstr. 2) After "X" miliseonds of copying the data to the buffer, 2nd thread should get started & it should copy all the stuff that was dumped to buffer till date. Again after "X" milisonds the thread 2 should pause & wait for "X" ms.



我该如何实现。在这里,第一个线程是运行我的库代码的默认线程。
如何使用C++实现此目的?

最佳答案

您可以使用std::mutexstd::condition_variable>发挥自己的优势。并且双缓冲区将锁定保持在最低限度。
std::condition_variable>是与std必须提供的事件最接近的事物,它的使用有点虚构,但是可以工作。

下面的示例使用一个双缓冲区,因此您可以在线程2保存到文件时继续进行数据缓冲,而不会锁定。

使用了std:condition_variable,因此您的应用程序可以退出而无需等待。仅当您希望应用程序立即退出时才需要这样做,否则可以使用计时器。对notify_all()的调用将防止wait_for()超时,并立即唤醒写入线程,因此它可以退出而无需等待超时发生。参见引用:http://en.cppreference.com/w/cpp/thread/condition_variable

header :

#include <mutex>

// a generic double buffer
template<typename _Data>
class DoubleBuffer
{
private:
std::mutex mutex_;

std::vector<std::vector<_Data>> storeBuffer_;
std::vector<std::vector<_Data>> saveBuffer_;

public:
void lock() { mutex_.lock(); }
void unlock() { mutex_.unlock();}

auto& GetStoreBuffer() { return storeBuffer_; }
auto& GetSaveBuffer() { return saveBuffer_; }
auto& Swap()
{
std::lock_guard<std::mutex> lock(mutex_);
std::swap(storeBuffer_, saveBuffer_);
}
};

在您的图书馆中:
#include <condition_variable>
#include <thread>
#include <chrono>
#include <mutex>
#include <vector>
#include <string>

// As an example, could be inside a class, or struct
static std::condition_variable exiting;
static std::mutex lk_exiting;

static DoubleBuffer<std::string> yourBuffer;

void FillLibraryDataStructure(std::vector<std::string> strings)
{
// the lock is only for the duration of a swap - very short at worst.
std::lock_guard<DoubleBuffer<std::string>> lock(yourBuffer);
yourBuffer.GetStoreBuffer().emplace_back(strings);
}

void StoreLoop()
{
for(;;)
{
{ // wait_for() unlocks lk_exiting, doc says lock should
// be set before cv is triggered.
std::unique_lock<std::mutex> lk_exiting;
if (std::cv_status::no_timeout == exiting.wait_for(lk_exiting, 60s))
break; // app is exiting
}

yourBuffer.Swap();

auto& stringsToSave = GetSaveBuffer();
// save... You do have plenty of time.
}
}

// as an example. A destructor would be a good place for this
void Exit_Application()
{
// stops the wait_for operation in StoreLoop()
exiting.notify_all();
}

关于c++ - 多线程和共享资源:使用C++定期将数据从缓冲区(数据结构)复制到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44888657/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com