Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC's behaviour with std::async(std::launch::async) vs. Clang's behaviour

Does anyone has experience with the rather new std::async? We are currently implementing a parallel file parser, which reads a file chunk and passes this chunk to an async function.

Using Clang (v3.0) this way works really fine with the default std::async policies (implementation dependent). On a two core machine, it fires up to 4 threads what works really well.

But with GCC (v4.7), the file reading thread does not spawn any new threads, making the program in the end completely sequential.

Using std::launch::async, both versions are pretty much doing the same (what should be the case).

Does anyone know the status of the current of GCC's c++11 threading capabilities? Or might this be an error in our implementation?

Short code:

while (readNewChunk()) {
    Chunk &chunk = fileReader_.getChunk(); //reading the file
    ChunkLoader *chunkLoader = new ChunkLoader();
    auto ftr = std::async(std::launch::async, &ChunkLoader::createDictionaries, chunkLoader);
    dictCreationFutures_.push_back(std::move(ftr));
}
like image 538
Bouncner Avatar asked Apr 07 '12 23:04

Bouncner


People also ask

Does std:: async use a thread pool?

std::async when std::launch::async policy is in effect, runs the function in a thread-pool thread. Thread-pool threads may be reused. As a result, thread-local variables may have previous values for subsequent runs.

Does std async spawn a new thread?

= 0), then async converts f and args... the same way as by std::thread constructor, but does not spawn a new thread of execution.

How does std:: async work?

By default, std::async executed immediately its work package. The C++ runtime decides if the calculation happens in the same or a new thread. With the flag std::launch::async std::async will run its work package in a new thread.

What is async C++?

As the name indicates, C++ async is a function template fn, which takes functions or function objects as arguments (basically called callbacks) and runs them asynchronously. It returns the std:: the future object which is used to keep the result of the above function. The result is stored in the shared state.


1 Answers

EDIT2: I'll explain a bit more.

std::async promises a 'future;' that is: when you want it, it'll be there. It may be computed now, it may be computed when you ask for it, we're just promising it'll happen.

Like the poster below me notes, GCC defaults to deferred (which means, it'll fulfill that promise when it is asked for it, and probably not beforehand). The reason for this default is because GCC doesn't provide proper C++11 threading support yet. It doesn't have a good internal thread scheduler, among many other things. It is a bit of a hack. No, more like a bunch of a hack. In fact, if you write threaded code in C++11 on GCC, it's more so that when they DO implement features, it'll work right; right now, it mostly works right. I mean, you get the result in the end, right?

If you tell it to launch a thread, it will, because it's too stupid (at the moment) to realize that it can and should on its own (unlike CLang, which has a better internal thread scheduler currently).

EDIT: Seriously? Misinformed down-modding!

Here's my reference! http://gcc.gnu.org/projects/cxx0x.html . Note almost everything under 'concurrency' including 'memory model' is noted as NO . GCC.GNU.org. They are the authority on GCC you know.

Slightly edited from my comment:

I would really recommend using Boost. It won't be a big jump to proper C++11 support when GCC is ready. The new threading models in C++11 require a different memory layout than GCC or MSVC are using, and they aren't implemented much really yet.

like image 162
std''OrgnlDave Avatar answered Oct 05 '22 23:10

std''OrgnlDave