Concurrent computing: Difference between revisions
Content added Content deleted
m (Updated whitespace.) |
m (Removed output as specified in task and updated whitespace.) |
||
Line 237: | Line 237: | ||
{{works with|C++11}} |
{{works with|C++11}} |
||
The following example compiles with GCC 4.7. |
The following example compiles with GCC 4.7. |
||
<code>g++ -std=c++11 -D_GLIBCXX_USE_NANOSLEEP -o concomp concomp.cpp</code> |
<code>g++ -std=c++11 -D_GLIBCXX_USE_NANOSLEEP -o concomp concomp.cpp</code> |
||
<lang cpp>#include <thread> |
<lang cpp>#include <thread> |
||
#include <iostream> |
#include <iostream> |
||
Line 245: | Line 243: | ||
#include <random> |
#include <random> |
||
#include <chrono> |
#include <chrono> |
||
⚫ | |||
std::random_device rd; |
|||
⚫ | |||
⚫ | |||
{ |
|||
std:: |
std::mt19937 eng(rd()); |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
std::chrono::milliseconds duration(dist(eng)); |
|||
⚫ | |||
⚫ | |||
⚫ | |||
std:: |
std::this_thread::sleep_for(duration); |
||
⚫ | |||
}); |
|||
⚫ | |||
⚫ | |||
std::this_thread::sleep_for(duration); |
|||
⚫ | |||
⚫ | |||
t.join(); |
|||
⚫ | |||
⚫ | |||
return 0; |
return 0; |
||
}</lang> |
}</lang> |
||
Output: |
|||
<pre>Enjoy |
|||
Code |
|||
Rosetta</pre> |
|||
{{libheader|Microsoft Parallel Patterns Library (PPL)}} |
{{libheader|Microsoft Parallel Patterns Library (PPL)}} |
||
<lang cpp>#include <iostream> |
<lang cpp>#include <iostream> |
||
// MSVC++ |
|||
#include <ppl.h> |
|||
void a(void) { |
void a(void) { |
||
std::cout << "Eat\n"; |
|||
} |
|||
⚫ | |||
void b(void) { |
|||
std::cout << "At\n"; |
|||
⚫ | |||
} |
|||
{ |
|||
void c(void) { |
|||
⚫ | |||
} |
|||
⚫ | |||
// function pointers |
// function pointers |
||
Concurrency::parallel_invoke(&a, &b, &c); |
Concurrency::parallel_invoke(&a, &b, &c); |
||
Line 295: | Line 286: | ||
return 0; |
return 0; |
||
}</lang> |
}</lang> |
||
Output: |
|||
<pre> |
|||
Joe's |
|||
Eat |
|||
At |
|||
Enjoy |
|||
Code |
|||
Rosetta |
|||
</pre> |
|||
=={{header|Cind}}== |
=={{header|Cind}}== |