dimanche 28 juin 2015

Use multiple cores on windows with cygwin gcc 4.9.2 and std::async

Right so I can create a bunch std::future objects using std::async with the std::launch::async policy in a c++ project using CLion + cygwin with gcc 4.9.2. However they don't seem to use more than one core.

When I call std::this_thread::get_id() they all return different values. I would think that separate threads would use more that one core however this doesn't seem to be happening. Is there a known issue in cygwin/gcc for this or is there more setup that needs to be done to make the threads use multiple cores?

The code that creates the futures basically looks like this:

for (directory_iterator itr(path); itr != end_itr; ++itr) {
    futures.push_back(std::async(std::launch::async, &load,this,itr->path().string()));
}
for (auto f : futures){
    auto fileData = f.get(); 
    //store fileData for later and go on with life...
}

futures is a std::vector<future<shared_ptr<WidgetThingy>>> and directory_iterator is a Boost directory iterator and &load basically reads a file using a Boost property_tree (each async makes its own property_tree) and returns a class containing the files information there are a more than 200 files and they are all large but each can be processed separately. After creating the std::futures I just loop through the vector of futures and call get() on each one which should block the main thread until that std::future is completed but the others should still be running in the background right?

If I use the std::launch::deferred policy the loads happen in ~20-30 seconds for the ~200 files I'm testing with and using the std::launch::async policy it can take ~3-5 minutes for the same files. I was hoping to get a performance increase here and the number of files it must load on startup is expected to be a lot larger than 200; have I maybe misunderstood what std::async is for?

Aucun commentaire:

Enregistrer un commentaire