Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dispatch_async() from Grand Central Dispatch and std::async from C++11

I have got some experience using GCD for concurrency and removing explicit locks and threads.

C++11 provides std::async, which seems to provide some similar features(, I'm not a C++ expert, please don't blame me for mistakes on it).

Putting aside arguments on flavours and language preferences, is there any benchmark comparing the two for their performance, especially for platforms like iOS?

Is c++11's std::async worth trying from a practical perspective?

EDIT:

As stackmonster answered, C++11 does not provide something exactly like a dispatch queue per se. However, isn't it possible to make an ad-hoc serial queue with atomic data structures(, and arguable lambda functions) to achieve this?

like image 825
ZhangChn Avatar asked Oct 20 '12 16:10

ZhangChn


2 Answers

C++ 11 std::async is not nearly as sophisticated as grand central dispatch.

Its more analogous to the async concurrency model provided by the java.util.concurrent package providing templates for callbacks but with no built in performance advantages.

I would say that the difference between them is simply this.

A callback template has no particular performance characteristics. GCD is all about performance, and threading/multiplexing those callbacks to reduce thread creation overhead and allowing queuing and task dependencies and thread pooling.

The launch policies of std::async do not compare in their sophistication to GCD and are not implementation portable.

Im not really sure what a benchmark between the two would really prove since they are not that really similar.

like image 100
deleted_user Avatar answered Nov 04 '22 02:11

deleted_user


As others have already pointed out, this comparison is generally meaningless due to its apples/oranges nature, though if you really wanted to I suppose you could test std::async and std::future against some GCD based futures implementation you cobble together yourself and see which provides futures the quickest for a known set of computations. Might be vaguely interesting, but you'd have to be the one to do it since the experiment is likely too strange and esoteric to be of interest to anyone else. :-)

like image 33
jkh Avatar answered Nov 04 '22 01:11

jkh