Concurrent computing: Difference between revisions

Updated to only use standard libraries. Removed comments as language should allow simple enough code.
m (Updated whitespace.)
(Updated to only use standard libraries. Removed comments as language should allow simple enough code.)
Line 210:
int main() {
std::random_device rd;
// mt19937 generator with a hardware random seed.
std::mt19937 eng(rd());
std::uniform_int_distribution<> dist(1,1000);
std::vector<std::thread> threads;
for (const auto& str: {"Enjoy\n", "Rosetta\n", "Code\n"}) {
// between 1 and 1000ms per our distribution
std::chrono::milliseconds duration(dist(eng));
threads.emplace_back([str, duration]() {
Line 222 ⟶ 220:
});
}
for (auto& t: threads)
t.join();
return 0;
}</lang>
{{libheader|Microsoft Parallel Patterns Library (PPL)}}
<lang cpp>#include <iostream>
// MSVC++
#include <ppl.h>
void a(void) {
std::cout << "Eat\n";
}
void b(void) {
std::cout << "At\n";
}
void c(void) {
std::cout << "Joe's\n";
}
int main() {
// function pointers
Concurrency::parallel_invoke(&a, &b, &c);
 
// C++11 lambda functions
Concurrency::parallel_invoke(
[]{ std::cout << "Enjoy\n"; },
[]{ std::cout << "Rosetta\n"; },
[]{ std::cout << "Code\n"; }
);
return 0;
}</lang>