Load big file asynchronously using c++ [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Hi I dnt have any knowledge on Multithreading or Parallel programing.
I need to load multiple file for an application, in which the load time does not affect the application or response to user.
I used CreateThread with that I cannot able to load data to a class variable.
Any guidance of how to do this in VC++ will be a great help.
Thanks in Advance !!
For example,
My application is streaming content meantime I need to load a big image to to class variable (Bitmap), which should not affect the streaming i.e without pause.

Modern C++ allows you to use hi level abstract features such as std::future:
struct Data {
// file name just for info
std::string file_name;
// here is data from file ...
static Data load(const std::string& name) {
Data data{ name };
// todo load from file
return std::move(data);
}
};
std::vector<std::string> names = { "file1.txt", "file2.txt", "file3.txt" };
std::vector<std::future<Data>> results;
for (const auto& name : names) {
// load from the name file asynchronously
auto future = std::async(std::launch::async, &Data::load, std::ref(name));
results.emplace_back(future);
}
// gather result
for (auto& future : results) {
Data& data = future.get();
// todo use data from the file object
}

Related

How to implement file reading and creating into a structure [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I don’t know how to implement reading data and put it into the structure
There is a config.txt file, it stores data like
someBigText=ASDSDdasdsa (can be more +1000 simbol)
isOkay=true
myAge=24
struct Config {
std::string Name;
std::string StringValue;
};
assuming your config is splitted in sections like:
[myImportantSection]
someBigText = "foo tha can be more +1000 simbols"
isOkay = true
myAge = 24
and assuming you are using boost:
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
boost::property_tree::ptree myPtree;
boost::property_tree::ini_parser::read_ini("config.txt", myPtree);
auto text{myPtree.get<std::string>("myImportantSection.someBigText")};
auto isOk{myPtree.get<bool>("myImportantSection.isOkay")};
auto age{myPtree.get<int>("myImportantSection.myAge")};
struct Config
{
std::string text{};
bool ok{false};
int age{0};
};
Config myConfig;
myConfig.text = text;
myConfig.ok = isOk;
myConfig.age = age;

C++ single-function variable placement [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I'm writing a class that reads data from a file. The project is still in development, and it's likely that I'll change the file name or path later on, so I've stored it in a std::string for quicker editing.
Given that the file name is going to be used several times in a function, but is only going to be used in one function, is there a canonical cpp rule about where I should define the variable?
//don't know where I'll define this
std::string file_name = "path/to/file.foo";
//a.h file
class A {
public:
void fileFunc();
private:
//do i define it here?
};
//a.cpp file
A::fileFunc() {
//or do i define it here?
std::ifstream in(file_name);
if(in) {
//do things
}
else {
std::cerr << "couldn't open " << file_name;
}
}
Keeps all information close to thiers use.
It will help the readability and the performance. See: https://en.wikipedia.org/wiki/Locality_of_reference
So
A::fileFunc() {
const std::string file_name = "path/to/file.foo"; // pls use const when you can
...
or
A::fileFunc(const std::string& file_name) {
...
BTW, I think this should be on https://codereview.stackexchange.com/, not stackoverflow.

C++ serializing an object containing an array of other objects [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have the object listed below.
class Customer
{
private:
std::string customerName;
std::string customerLastName;
std::string customerIdentityNumber;
std::vector<std::unique_ptr<Account>> customerAccounts;
}
How would one go about serializing this object? I've tried finding examples but these are all using some complex libraries. Surely there must be an easier way?
Coming from Java this is new to me.
I really recommend a serialization library such as boost::serialization
Its a great library, easy to use, extremely fast, and has much more than just this!
It's exactly what you're looking for.
I prefer a very simple and basic implementation. Lets assume that Serialize() function has already been implemented for Account class.
The implementation of Serialize() function of Customer class can be:
void Customer::Serialize(Archive& stream)
{
if(stream.isStoring()) //write to file
{
stream << customerName;
stream << customerLastName;
stream << customerIdentityNumber;
stream << customerAccounts.size(); //Serialize the count of objects
//Iterate through all objects and serialize those
std::vector<std::unique_ptr<Account>>::iterator iterAccounts, endAccounts;
endAccounts = customerAccounts.end() ;
for(iterAccounts = customerAccounts.begin() ; iterAccounts!= endAccounts; ++iterAccounts)
{
(*iterAccounts)->Serialzie(stream);
}
}
else //Reading from file
{
stream >> customerName;
stream >> customerLastName;
stream >> customerIdentityNumber;
int nNumberOfAccounts = 0;
stream >> nNumberOfAccounts;
customerAccounts.empty(); //Empty the list
for(int i=0; i<nNumberOfAccounts; i++)
{
Account* pAccount = new Account();
pAccount->Serialize(stream);
//Add to vector
customerAccounts.push_back(pAccount);
}
}
}
The code is self-explanatory. But idea is to archive count and then every element. This help while deserializing from file.

c++ using scope resolution operator in function call parameter [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Can someone please point me to an explanation what e.g. QIODevice::WriteOnly actually does?
full line of code:
file.open(stderr, QIODevice::WriteOnly);
from that link
thanks
According to the documentation for the QIODevice class, WriteOnly is as enum constant with value 2. It indicates that the device is open for writing.
I believe that the following example for enum hack will be useful to you.
class MyClass1 {
public:
enum { SIZE=10 };
};
class MyClass2 {
public:
enum { SIZE=20 };
};
int main() {
cout << MyClass1::SIZE << "\t" << MyClass2::SIZE << endl;
}
QIODevice::WriteOnly is just a flag, you're saying that you want to open the file only for writing.
If you would want only to read the file, QIODevice::ReadOnly would be the necessary flag to use.
And to read and write use flag: QIODevice::ReadWrite:
file.open(stderr, QIODevice::ReadWrite);

Decouple visualization methods and application [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
this is my first question on SO, so please bear with me.
We develop an application, which gathers data, and we have methods that let us visualize the data in various ways. With growing number of methods, we decided to separate the application and the visualization methods. I'm wondering what is the best way to accomplish this. I've come up with the following code, which somewhat tries to separate the two, but still ...
Is there a better way to do it?
// forward declaration
class App;
// Interface to all visualization methods
struct Methods {
virtual void show(App * a) = 0;
};
// Some visualization method
struct Method0 : public Methods {
void show(App * a) {
a->getData();
}
};
class App {
public:
vector<Methods *> methods;
void run() {
// draw all registered methods
for (auto m : methods)
m->show(this);
}
int getData() {
// parse and precompute data (time-consuming, thus do it only once)
// return the required data (not just an int..)
return 42;
}
};
void main() {
App a;
// register some methods
a.methods.push_back(new Method0());
// run the application
a.run();
// clean up
for (auto m : a.methods) delete(m);
}
EDIT
I think Alexander and Petr pointed me in the correct direction, thank you. I'll follow Petr's suggestion and try to separate the data into another class.
Addressing the comment by Spektre:
Developed on Windows (MSVC), otherwise platform independent.
The visualization is mostly static and changes based on user input. I guess 10 updates per second is an upper bound on the refresh rate.
What do you mean by data transfer times?
Memory is not an issue.
The data is a bunch of vectors of objects holding other vectors of objects, 5 dimensions in total.
One visualization is similar to ROC curve, containing several curves, so we need to traverse part/all the dimensions and compute some statistics. The result is shown in the following figure.
What you have there already looks quite good. As you have probably already assumed, you are not the first person to have this kind of problem. The standard solution for the separation of your data from your visualization is known as the Model View Controller Pattern (MVC), which not only decouples the presentation of your data from the data itself, but also allows for simple manipulation of the data from the display.
If you just want to display your data, then you might want to have a look at the Observer Pattern. Then again, what you have is already quite close to this pattern.
In addition to an answer by Alexander, I will mention that actually you did not completely separated data and visualization. The Application class still knows both about the internal structure of data and about the vector of visualization methods. What you should better do is to have a separate class, say Data, the will be doing all the computations you need, and then have the main class (App for example) that will just handle registration of methods and passing data to them.
Something like
class Data;
struct Methods {
virtual void show(Data * a) = 0;
};
struct Method0 : public Methods {
void show(Data * d) {
d->getData();
}
};
class Data {
public:
int getData() {
// parse and precompute data (time-consuming, thus do it only once)
// return the required data (not just an int..)
return 42;
}
}
class App {
public:
vector<Methods *> methods;
Data* data;
void run() {
// draw all registered methods
for (auto m : methods)
m->show(data);
}
};